Вопрос

I'm trying to get an access token from Google API in my Ruby on Rails app, as part of an overall goal of setting up a raketask. I am able to get an Auth Code fine, but when I make a post request to get an access token, I am getting a 302 error. I'll describe my current code first, and afterward list how I've tried to solve the problem so far.

Current code:

#users_controller
def auth_access
client = Signet::OAuth2::Client.new(
      :authorization_uri => 'https://accounts.google.com/o/oauth2/auth',
      :token_endpoint_uri => 'https://accounts.google.com/o/oauth2/token',
      :client_id => ENV['OAUTH_CLIENT_ID'],
      :client_secret => ENV['OAUTH_CLIENT_SECRET'],
      :scope => 'https://www.googleapis.com/auth/analytics.readonly',
      :redirect_uri => 'http://localhost:3000/google/auth_callback'
    )

    redirect_to client.authorization_uri.to_s
end

This part works fine so far. It redirects to the consent page, and when the user agrees it then redirects them to the page with the auth code in the url parameters. Next I take that auth code and try to make a POST request to API for an access token:

#users_controller
def auth_callback
http = Net::HTTP.new('accounts.google.com')

    path = '/o/oauth2/token'
    data = "code=#{params['code']}&client_id=#{ENV['OAUTH_CLIENT_ID']}&client_secret=#{ENV['OAUTH_CLIENT_SECRET']}&redirect_uri=http://localhost:3000/auth_final&grant_type=authorization_code"
    response = http.post(path, data)
end

This when I run into a problem. The Google API returns a 302, and includes a message saying something akin to "we moved to 'https://accounts.google.com/o/oauth2/token'".

Here's how I've tried to fix the problem so far:

  1. I assumed that the problem was that the http.post method is making a call to an http and not https.

I've tried including

http.use_ssl = true
http.ssl_version = :SSLv3

This returns the error "SSL_connect returned=1 errno=0 state=SSLv3 read server hello A: wrong version number".

I can take a guess at what this means, but I am still unsure of what the actual problem is and how to solve it. Googling the error message has not been a help.

  1. In a similar vein, I tried using gems to make the https call for me, in particular HTTParty and Typheous, although I was not able to make any progress with them (and am still not even sure that it's an http/https problem).

  2. I've tried using the Signet-Rails gem. This was the most productive method by far, making a successful API call and returning the information. However, it either wasn't saving the refresh token or I cannot find where it is being saved. As I need access to that token to run the rake tasks, I gave up on Signet-Rails.

  3. I tried using Legato, and was constantly running into various problems. Overall, Legato left me with the impression that it did not integrate getting the auth code, consent and tokens into the app, instead requiring the developer to set those up in advance outside of the app's scope. I want to be able to set up the auth code as part of the app. If I am understanding Legato properly, then it is not the gem I need.

I've also tried other various odds and ends but to no avail. The above solutions were the tactics I kept coming back to. Primarily I'm looking for an answer to what is going wrong in my code, and what is the best avenue to fix it (and if I was going down the right track with any of my attempted solutions above, which one?)

Thanks for taking the time to read this and answer!

(on a complete sidenote, those last three list items should be 2, 3, 4, but the stackoverflow text editor thinks it knows better than me...)

Это было полезно?

Решение

Specify the port:

http = Net::HTTP.new('accounts.google.com', 443)

Source: SSL Error on HTTP POST (Unknown Protocol)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top