Ruby OAuth Nightmare: Using the Contacts API

I spent the last few days bumping my head against the wall, supporting the ability to add a contact to the Google Contacts API in my Rails 3 application. Despite many false starts, I finally made some progress by using the Ruby OAuth stone and following the instructions here: http: //everburning.com/news/google-analytics-oauth-and-ruby-oh-my/

When I follow this in the console, I get more than in my Rails application. I can create an access token, authenticate a Google service with a specific area of ​​the contact APIs, and apply the oauth_verifier token to get the access token. But when the time comes for data entry, I get this error:

response = at.post("https://www.google.com/m8/feeds/contacts/default/full", gdata)
 => #<Net::HTTPUnauthorized 401 Unknown authorization header readbody=true> 

Where does the header "readbody = true" come from and how can I get rid of it?

But this is worse in a Rails application. I have one controller action ("googlecontacts") that creates a request token and leads the user to an authentication site with Google:

def googlecontacts

@card = Card.find_by_short_link(params[:id])

@consumer = OAuth::Consumer.new( 
  'anonymous', 
  'anonymous', 
  { 
    :site => 'https://www.google.com', 
    :request_token_path => '/accounts/OAuthGetRequestToken', 
    :access_token_path => '/accounts/OAuthGetAccessToken', 
    :authorize_path => '/accounts/OAuthAuthorizeToken', 
    :signature_method => 'HMAC-SHA1',
    :oauth_version => '1.0'
  })

@request_token = @consumer.get_request_token(
  {:oauth_callback => 'http://monkey.dev/cards/google_auth?redir='+@card.short_link}, 
  {:scope => "https://www.google.com/m8/feeds/"}
)

session[:request_token] = @request_token

redirect_to @request_token.authorize_url

end

This seems to work; I get a work request token object and the user is sent to the Google service for authentication. The callback URL ("google_auth") must use the oauth_verifier token to create the access token. Here is the start of the controller:

def google_auth

   @access_token = session[:request_token].get_access_token(:oauth_verifier=>params[:oauth_verifier])

And here where he is shit. The error in this last line is:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]

But the values ​​that are there - the session [: request_token] and params [: oauth_verifier] - are present and taken into account in this action! I can’t understand what is here.

, , , .: -)

.

.

+3
2

/ , , .. session["request_token"], session[:request_token]. , .

0

Unknown authorization header , , . oauth. , .

Signet gem - API Google Ruby.

Signet:

require 'signet/oauth_1/client'
require 'addressable/uri'
card = Card.find_by_short_link(params[:id])
callback = Addressable::URI.parse('http://monkey.dev/cards/google_auth')
callback.query_values = {'redir' => card.short_link}

client = Signet::OAuth1::Client.new(
  :temporary_credential_uri =>
    'https://www.google.com/accounts/OAuthGetRequestToken',
  :authorization_uri =>
    'https://www.google.com/accounts/OAuthAuthorizeToken',
  :token_credential_uri =>
    'https://www.google.com/accounts/OAuthGetAccessToken',
  :client_credential_key => 'anonymous',
  :client_credential_secret => 'anonymous',
  :callback => callback
)

session[:temporary_credential] = (
  client.fetch_temporary_credential!(:additional_parameters => {
    :scope => 'https://www.google.com/m8/feeds/'
  })
)
redirect_to(client.authorization_uri)
0

Source: https://habr.com/ru/post/1768926/


All Articles