通过OAuth2,Ruby连接到Google时获得401响应

时间:2013-12-02 11:09:00

标签: ruby-on-rails gmail oauth-2.0 google-contacts

我正在尝试从我的网站访问Google通讯录中的所有联系人以进行邀请。 所以我正在使用OAuth2,但是当我第一次请求时,我遇到了access_token的401错误。 :(

请记住我在当地工作!!

这是我的代码。

 def authenticate
      # initiate authentication w/ gmail
      # create url with url-encoded params to initiate connection with contacts api
      # next - The URL of the page that Google should redirect the user to after authentication.
      # scope - Indicates that the application is requesting a token to access contacts feeds.
      # secure - Indicates whether the client is requesting a secure token.
      # session - Indicates whether the token returned can be exchanged for a multi-use (session) token.
      next_param = "http://localhost:3000/import/authorise" 
      scope_param = "https://www.google.com/m8/feeds/"  
      #session_param = "1"
      state="profile"
      client_id="535946964206.apps.googleusercontent.com"
      root_url = "https://accounts.google.com/o/oauth2/auth"
      response_type="token"
      #TO FIND MORE ABOUT THESE PARAMETERS AND THERE MEANING SEE GOOGLE API DOCS.
      query_string = "?scope=#{scope_param}&state=#{state}&redirect_uri=#{next_param}&response_type=#{response_type}&client_id=#{client_id}"

      #render :text=> root_url + query_string and return
    #  redirect_to "https://accounts.google.com/o/oauth2/auth?
    #scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&
    #state=%2Fprofile&
    #redirect_uri=http://localhost:3000/import/authorise&
    #response_type=token&
    #client_id=535946964206.apps.googleusercontent.com
    #"
      redirect_to root_url + query_string
    end

这是用于身份验证现在我获得了访问令牌

的响应
http://localhost:3000/import/authorise#state=profile&access_token=ya29.1.AADtN_WPohnlKYBH16kFrqEcYUTZ558WDICpqCqiz1mwBKESFr5PTiJFA5vsyoc&token_type=Bearer&expires_in=3600

现在接下来它会重定向到这里,这是我遇到错误的地方

# YOU WILL BE REDIRECTED TO THIS ACTION AFTER COMPLETION OF AUTHENTICATION PROCESS WITH TEMPORARY SINGLE USE AUTH TOKEN.
def authorise
  #FIRST SINGEL USE TOKEN WILL BE RECEIVED HERE..
  token = params[:token]


  #PREPAIRING FOR SECOND REQUEST WITH AUTH TOKEN IN HEADER.. WHICH WILL BE EXCHANED FOR PERMANENT AUTH TOKEN.
  uri = URI.parse("https://www.google.com")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  path = '/accounts/AuthSubSessionToken'
  headers = {'Authorization' => "AuthSub token=#{token}"}

  render :text=> http.get(path, headers) and return
  #GET REQUEST ON URI WITH SPECIFIED PATH...
  resp, data = http.get(path, headers)

  #ender :text=> data and return
  resp.code="200"
  #SPLIT OUT TOKEN FROM RESPONSE DATA.
  if resp.code == "200"
    token = ''
    data.split.each do |str|
    if not(str =~ /Token=/).nil?
      token = str.gsub(/Token=/, '')
    end
  end


return redirect_to(:action => 'import', :token => token)
else
redirect_to root_url , :notice => "fail"
end
end

此处上述函数在请求resp, data = http.get(path, headers)时,响应resp.code为401。我不知道我在跟https://gist.github.com/miketucker/852849使用现在没有使用的AuthSub的{{1}}示例有什么不对,所以我正在调整oauth2。 我已经在clode控制台上创建了应用程序并给出了返回URL和其他信息。

任何帮助或指向我其他工作示例,谢谢! 谢谢。

0 个答案:

没有答案