簡體   English   中英

如何使OAuth2與服務帳戶一起使用

[英]How to get OAuth2 working with a service account

我們需要從舊的預配置API遷移到新的Admin SDK。 問題是我們無法弄清楚如何使OAuth2與服務帳戶一起使用。 我們以以下代碼為例,但無法確定在哪里/如何創建key.p12文件。 我已使用cliendId,電子郵件地址和API密鑰創建了服務帳戶,該帳戶與開發服務器控制台中的應用綁定到了我的服務器。 如果有人可以幫助我了解如何創建key.p12文件,那么我很確定可以使它正常工作。

謝謝,

滑雪

// Set your client id, service account name, and the path to your private key.
// For more information about obtaining these keys, visit:
// https://developers.google.com/console/help/#service_accounts
const CLIENT_ID = 'insert_your_client_id';
const SERVICE_ACCOUNT_NAME = 'insert_your_service_account_name';

// Make sure you keep your key.p12 file in a secure location, and isn't
// readable by others.
const KEY_FILE = '/super/secret/path/to/key.p12';

// Load the key in PKCS 12 format (you need to download this from the
// Google API Console when the service account was created.
$client = new Google_Client();

... $ key = file_get_contents(KEY_FILE); $ client-> setClientId(CLIENT_ID); $ client-> setAssertionCredentials(新的Google_AssertionCredentials(SERVICE_ACCOUNT_NAME,array(' https://www.googleapis.com/auth/prediction '),$ key));

前往https://cloud.google.com/console
選擇你的項目
點擊“ API和驗證”
點擊“憑據”
點擊“創建新的客戶ID”
選擇“服務帳戶”
點擊“創建客戶端ID”
您的私鑰將被下載。
您的公鑰將被存儲並顯示在控制台中。
您將無法再次檢索私鑰。 Google只發送一次。 如果丟失,則必須重新執行該過程,並更新應用程序中的相關設置。
如果已回答此問題,請在此標記。

請參閱google oauth2如何獲取服務帳戶的私鑰 -這實際上已隨新版本的Google Cloud Console發生了變化,但是答案是最新的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM