SSL Error in Rails 3.2.3 while using Google Oauth2 client to access API

大兔子大兔子 提交于 2020-01-01 16:48:28

问题


I'm fairly new to OAuth2 and I'm trying to access a user's Blogger account through the Google API with Omniauth and the Google API client. I'm using the following:

  • Rails 3.2.3
  • Ruby 1.9.3
  • oauth2 (0.8.0)
  • omniauth (1.1.1)
  • omniauth-google-oauth2 (0.1.13)
  • google-api-client (0.4.6)

When I first tried to authenticate a user with Google credentials, I received the following error:

SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed

However, when I added the specific path to my CA certs in the initializer, the error went away:

provider :google_oauth2, ENV['GOOGLE_APP_ID'], ENV['GOOGLE_SECRET'], {
  access_type: "offline",
  approval_prompt: "",
  scope: 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/blogger',
  client_options: { ssl: { ca_file: cert_path } }
}

So, now my user can login with their Google credentials with no problem. The issue I'm now seeing is that when I try to use the access token (received from Google during user authentication) to access the Blogger API, I get the SSL error again. This is the code I'm using to access the API:

token = auth.first.oauth_token.access_token   # access token received during authentication

client = Google::APIClient.new
client.authorization.access_token = token
service = client.discovered_api('blogger', 'v3')
result = client.execute(
     :api_method => service.blog_list.list,
     :parameters => {},
     :headers => {'Content-Type' => 'application/json'})

The error is being generated in the line service = client.discovered_api('blogger', 'v3')

I've been banging my head against the wall for a while now, anyone have any ideas?


回答1:


After taking a break, some more Googling, and more head banging, I came across the wonderfully elegant solution at https://gist.github.com/867550. Setting the SSL_CERT_FILE environment variable and restarting my machine fixed the issue. Oh yeah, did I forget to mention I'm developing on a Windows machine? Good times.



来源:https://stackoverflow.com/questions/12520314/ssl-error-in-rails-3-2-3-while-using-google-oauth2-client-to-access-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!