arsduo/koala · error · Koala::Facebook::AuthenticationError

Write operations require an access token

Error message

Write operations require an access token

What it means

put_connections raises Koala::Facebook::AuthenticationError ('Write operations require an access token') when access_token is nil (lib/koala/api/graph_api_methods.rb:159). Every write to the Graph API — posts, comments, likes, photos, videos — must identify an authenticated user or app, so Koala refuses the call before any HTTP traffic. The guard sits beneath the whole write family: put_object, put_picture, put_video, put_wall_post, put_comment and put_like all raise this same error through it.

Source

Thrown at lib/koala/api/graph_api_methods.rb:159

      # @example
      #         graph.put_connections("me", "feed", :message => "Hello, world")
      #         => writes "Hello, world" to the active user's wall
      #
      # Most write operations require extended permissions. For example,
      # publishing wall posts requires the "publish_stream" permission. See
      # http://developers.facebook.com/docs/authentication/ for details about
      # extended permissions.
      #
      # @param id (see #get_object)
      # @param connection_name (see #get_connection)
      # @param args (see #get_connection)
      # @param options (see #get_object)
      # @param block (see Koala::Facebook::API#api)
      #
      # @return a hash containing the new object's id
      def put_connections(id, connection_name, args = {}, options = {}, &block)
        # Posts a certain connection
        raise AuthenticationError.new(nil, nil, "Write operations require an access token") unless access_token

        graph_call("#{id}/#{connection_name}", args, "post", options, &block)
      end

      # Delete an object's connection (for instance, unliking the object).
      #
      # @note (see #get_connection)
      #
      # @param id (see #get_object)
      # @param connection_name (see #get_connection)
      # @args (see #get_connection)
      # @param options (see #get_object)
      # @param block (see Koala::Facebook::API#api)
      #
      # @return (see #delete_object)
      def delete_connections(id, connection_name, args = {}, options = {}, &block)
        # Deletes a given connection
        raise AuthenticationError.new(nil, nil, "Delete requires an access token") unless access_token

View on GitHub (pinned to 47d052063e)

Solutions

  1. Build the API with the poster's token before any write: Koala::Facebook::API.new(user.facebook_token)
  2. When posting as a Page, fetch and use the Page access token (get_page_access_token) instead of assuming the user token works everywhere
  3. Validate the token is present (and unexpired) before enqueueing write jobs
  4. Rescue Koala::Facebook::AuthenticationError around user-facing writes: clear the stale token, re-run OAuth, retry the post once

Example fix

# before
api = Koala::Facebook::API.new
api.put_wall_post('Hello') # => AuthenticationError: Write operations require an access token

# after
api = Koala::Facebook::API.new(user.facebook_token)
api.put_wall_post('Hello', {link: 'https://example.com'})
Defensive patterns

Strategy: validation

Validate before calling

return reauthenticate_user! unless api.access_token
api.put_connections('me', 'feed', message: text)

Try / catch

begin
  api.put_wall_post(text)
rescue Koala::Facebook::AuthenticationError
  user.facebook_token = nil
  redirect_to login_path # re-run OAuth, then retry the post once
end

Prevention

When it happens

Trigger: api.put_connections('me', 'feed', message: 'hi'), api.put_wall_post('Hello'), api.put_picture(file), api.put_comment(id, 'nice'), api.put_like(id) — any write issued on a Koala::Facebook::API instance constructed without an access token.

Common situations: Posting features run for a user whose OAuth token expired and was stored as nil; writing to a Page feed without ever fetching a Page token; delayed jobs enqueued before the token was assigned; test suites building a bare API and expecting HTTP stubs to answer writes.

Related errors


AI-assisted analysis of arsduo/koala@47d052063e (2026-08-23). Data as JSON: /api/errors/32de1d6996649b85. Report an issue: GitHub.