Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 通过OAuth2和Ruby连接到Google时获得401响应_Ruby On Rails_Gmail_Oauth 2.0_Google Contacts Api - Fatal编程技术网

Ruby on rails 通过OAuth2和Ruby连接到Google时获得401响应

Ruby on rails 通过OAuth2和Ruby连接到Google时获得401响应,ruby-on-rails,gmail,oauth-2.0,google-contacts-api,Ruby On Rails,Gmail,Oauth 2.0,Google Contacts Api,我正试图从我的网站访问所有来自谷歌联系人的联系人,以进行邀请。 所以我使用的是OAuth2,但是当我第一次请求时,我得到了一个401错误,带有访问令牌:( 记住,我在当地工作 这是我的密码 def authenticate # initiate authentication w/ gmail # create url with url-encoded params to initiate connection with contacts api # next

我正试图从我的网站访问所有来自谷歌联系人的联系人,以进行邀请。 所以我使用的是OAuth2,但是当我第一次请求时,我得到了一个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)
时,response
resp.code
是401。我不知道我在跟踪什么
https://gist.github.com/miketucker/852849
使用AuthSub的示例,该示例现在未使用,因此我正在修改oauth2。 我已经在clode控制台上制作了应用程序,并给出了返回url和其他信息

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

您的请求标题设置正确吗?