2013-09-21 51 views
0

我嘗試使用google_drive使用login_with_oauth2,但我不明白什麼是#google_drive.rb文檔授權碼谷歌驅動紅寶石API登錄,什麼是「授權碼」

client = OAuth2::Client.new(
    "522807807986-gjotv2np4tdqp4do8sq0gds0p2bqugtf.apps.googleusercontent.com", 
    'fmWlfzejvx_UtS3CKq2Sl-WQ', 
    :site => "https://accounts.google.com", 
    :token_url => "/o/oauth2/token", 
    :authorize_url => "/o/oauth2/auth" 
) 

auth_url = client.auth_code.authorize_url(
    :redirect_uri => "urn:ietf:wg:oauth:2.0:oob 
http://localhost" 
) 

# Redirect the user to auth_url and get authorization code from redirect URL. 

authorization_code = '' 
auth_token = client.auth_code.get_token(
    authorization_code, :redirect_uri => "urn:ietf:wg:oauth:2.0:oob 
http://localhost") 
session = GoogleDrive.login_with_oauth(auth_token.token, 'http://localhost:8087') 

回答

0

OAuth2是兩步授權機制。 AFAIU,你的代碼只需從official documentation複製粘貼,但不幸的是這不是一個可用的代碼。這只是一個例子。看看註釋標記紅色有:這是非常重要的:

# Redirect the user to auth_url and get authorization code from redirect URL. 

你要指定正確的參照通知的URI谷歌服務器與發送響應您的authorization_code(現在有規定localhost,這是有問題讓谷歌瞭解你的知識產權向你發送反饋意見。)

總結:你向Google提供憑證,它用JSON回覆,包含你的authorization_code到你指定的地址。在此階段成功完成後,您可以繼續請求auth_token

+2

我可以通過在終端鍵入google-chrome auth_url手動獲取authorization_code,如何自動獲取它? – user1637972