2013-01-15 33 views
6

無效的授予我想創建一個應用程序,我只是直接連接到我的日曆...但我不想參與重新-authenticating。我只想編碼認證一次,並完成它。當試圖授權服務器到服務器類型的應用程序在Ruby日曆訪問Google日曆

的驗證代碼如下:

key = Google::APIClient::PKCS12.load_key(SERVICE_ACCOUNT_PKCS12_FILE_PATH, PASSWORD) 
asserter = Google::APIClient::JWTAsserter.new(
    SERVICE_ACCOUNT_EMAIL, 
    'https://www.googleapis.com/auth/calendar', 
    key) 
@client = Google::APIClient.new 
@client.authorization = asserter.authorize#(SERVICE_ACCOUNT_EMAIL) 
@client 

SERVICE_ACCOUNT_...PKCS .文件是與我的末日應用沿着目錄的根目錄。
我在我的Gmail帳戶上啓用了Google Calendar API。

我一直在尋找認證的方法在這裏: https://code.google.com/p/google-api-ruby-client/(經授權,在那裏談論服務器到服務器的通信)

$ ruby server.rb 
Faraday: you may want to install system_timer for reliable timeouts 
/home/me/.rvm/gems/ruby-1.8.7-p371/gems/faraday-0.8.4/lib/faraday/utils.rb:16: warning: already initialized constant KeyMap 
/home/me/.rvm/gems/ruby-1.8.7-p371/gems/faraday-0.8.4/lib/faraday/utils.rb:177: warning: already initialized constant DEFAULT_SEP 
W, [2013-01-15T09:52:11.371122 #28607] WARN -- : Google::APIClient - Please provide :application_name and :application_version when initializing the client 
/home/me/.rvm/gems/ruby-1.8.7-p371/gems/signet-0.4.4/lib/signet/oauth_2/client.rb:869:in `fetch_access_token': Authorization failed. Server message: (Signet::AuthorizationError) 
{ 
    "error" : "invalid_grant" 
} 
    from /home/me/.rvm/gems/ruby-1.8.7-p371/gems/signet-0.4.4/lib/signet/oauth_2/client.rb:882:in `fetch_access_token!' 
    from /home/me/.rvm/gems/ruby-1.8.7-p371/gems/google-api-client-0.6.1/lib/google/api_client/auth/jwt_asserter.rb:116:in `authorize' 
    from server.rb:32:in `build_client' 
    from server.rb:38 

還有什麼問題呢?爲什麼我得到這個錯誤? - 特別是因爲我幾乎從谷歌的網站上覆制了代碼。

UPDATE:

發現這一點:

invalid_grant trying to get oAuth token from google

說我應該這樣做: 「確保您在請求中指定ACCESS_TYPE =下線。」

我該如何用紅寶石做到這一點?沒有完全重做http請求?

+0

我在與到驅動器的連接類似的問題。今天早上剛剛開始。 – tbeseda

+0

你有沒有達成決議? – nurettin

+0

有沒有運氣?有同樣的問題 – hakunin

回答

0

這是一個工作代碼,除了最後一行外,它應該有實際擁有日曆(您)的用戶的電子郵件。我假設服務帳戶的電子郵件不是你的電子郵件。

client_asserter = Google::APIClient::JWTAsserter.new(
    config['client_email'], 
    PLUS_LOGIN_SCOPE, 
    key 
) 
$client.authorization = client_asserter.authorize(config['user_email']) 

此外,請確保您正確地創建服務帳戶。我使用了這個指南:https://developers.google.com/+/domains/authentication/delegation,這是用於Domain API的,但第一部分應該是相同的。

另外,你不需要另一個認證範圍?

下面是一個完整的例子(紅寶石),我得爲我工作:https://github.com/admitriyev/propellant/blob/master/main.rb

相關問題