2017-02-10 64 views
0

服務帳戶授權我想我的授權與谷歌的API使用服務帳戶谷歌日曆應用程序。現在我遇到了一個問題,出於某種原因,證書沒有被加載。谷歌日曆API不工作

這是我的授權碼:

scopes = 'https://www.googleapis.com/auth/calendar' 
authorization = Google::Auth.get_application_default(scopes) 
cal = Google::Apis::CalendarV3::CalendarService.new 
cal.authorization = authorization 
cal.authorization.fetch_access_token! 

下面是我收到錯誤消息: enter image description here

我有client_secrets.json檔案在我的ENV變量在谷歌的文檔中描述。我還將該文件中的每個元素都視爲ENV變量,因爲它是在Google認證代碼中指定的。

任何人都可以提供任何有關獲得授權工作的見解嗎?

回答

0

Using OAuth 2.0 for Server to Server Applications中強烈建議您使用庫,例如​​Google API客戶端庫。

正如Creating a service account討論,

如果您的應用程序無法在谷歌應用程序引擎或谷歌計算引擎運行,必須獲得在谷歌API控制檯這些憑據。要生成服務帳戶憑據或查看已生成的公用憑據,請執行以下操作:

  1. 打開Service accounts page。如果提示,請選擇一個項目。
  2. 點擊創建服務帳號
  3. 創建服務帳戶窗口中,鍵入服務帳戶的名稱,然後選擇提供新的專用密鑰。如果你想grant G Suite domain-wide authority服務帳戶,還可以選擇啓用摹套房域範圍內的代表團。然後點擊創建

您的新公鑰/私鑰對被生成並下載到您的機器;它充當此密鑰的唯一副本。您有責任安全地存儲它。

此外,我還建議您訪問Google Application Default Credentials以獲取有關默認憑證的更多詳細信息。

對於Ruby,默認憑據由Google API客戶端庫提供,版本爲0.1.0和更高版本。在調用應用程序代碼的應用程序的默認憑證,導入googleauth寶石,再傳給你需要訪問Google::Auth.get_application_default的範圍:

require 'googleauth' 
scopes = ['https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/devstorage.read_only'] 
authorization = Google::Auth.get_application_default(scopes) 

然後,使用的憑證按如下方式訪問的API服務:

require "google/apis/storage_v1" 
storage = Google::Apis::StorageV1::StorageService.new 
storage.authorization = authorization 

希望有幫助!

+0

很抱歉,但這個答案沒有幫助,因爲我已經做了這一切,並沒有解決問題。 – NeyLive