Search code examples
ruby-on-railstwitteromniauth

Omniauth 2.0 returning nil values (Twitter API)


Some context: I am new to software development and this is my first post on stackoverflow so if I get something wrong with how this is done please let me know.

Issue found whilst working through a GoRails tutorial (Rails for Beginners). I have searched for hours on stackoverflow and other online resources, revised my code many times and almost entirely given up hope

Summarize the problem:

Connecting a Twitter account using Omniauth 2.0, via the Twitter API v2, my omniauth return hash includes only nil values

I am unsure if Twitter is not providing this data intentionally due to my Essential access level or it is an error in my code that is the issue

The response from Twitter when I try to connect looks like this (excluded the first bit as it includes API keys):

@response=#<Net::HTTPForbidden 403 Forbidden readbody=true>> raw_info=#<OmniAuth::AuthHash errors=#<Hashie::Array [#<OmniAuth::AuthHash code=453 message="You currently have Essential access which includes access to Twitter API v2 endpoints only. If you need access to this endpoint, you’ll need to apply for Elevated access via the Developer Portal. You can learn more here: https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api#v2-access-leve">]>>> info=#<OmniAuth::AuthHash::InfoHash description=nil email=nil image=nil location=nil name=nil nickname=nil urls=#<OmniAuth::AuthHash Twitter="https://twitter.com/" Website=nil>> provider="twitter" uid="1369880016"

With my limited knowledge I believe the hash I am looking for this this part:

<OmniAuth::AuthHash::InfoHash description=nil email=nil image=nil location=nil name=nil nickname=nil

It looks like Twitter is returning nil values for what I am requesting (is that due to access level or my error)

In my OmniauthCallbacksController I have:

  def twitter
    Rails.logger.info auth

    Current.user.twitter_accounts.create(
      name: auth.info.name,
      username: auth.info.nickname,
      image: auth.info.image,
      token: auth.credentials.token,
      secret: auth.credentials.secret,
    )

    redirect_to twitter_accounts_path, notice: "Successfully connected your account"
  end

  def auth
    request.env['omniauth.auth']
  end

Describe what you’ve tried:

  • Change Twitter OmniAuth settings to accept both v1.1 and/or v2.0
  • Reissued API token
  • Created new accounts in both my app and Twitter
  • Copied source code direct from GitHub
  • Experimented with code from related stackoverflow posts

Is this like to be an error in my code or due to Twitter not returning the values

I am now at an impasse. I can continue with the tutorial but I do not want to be a half-arsed developer so I am determined to understand what I have done wrong. Please help if you can


Solution

  • OmniOAuth is still using the Twitter API OAuth 1.0A, and based on the error message it looks like it is probably trying to call /1.1/account/verify_credentials as part of the authentication flow. In order to use that endpoint, your project and app need to have Elevated access (Essential access only provides access to the v2 Twitter API at this time).

    If you get elevated access in order to access the older endpoints, this should help.