简体   繁体   English

如何使OAuth2与服务帐户一起使用

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

We need to migrate from the old provisioning API to the new Admin SDK. 我们需要从旧的预配置API迁移到新的Admin SDK。 Problem is that we cannot figure out how to get OAuth2 to work with a service account. 问题是我们无法弄清楚如何使OAuth2与服务帐户一起使用。 We are using the following code as the example, but cannot figure out where/how the key.p12 file is created. 我们以以下代码为例,但无法确定在哪里/如何创建key.p12文件。 I have the service account created with a cliendId, email address, and API key tied to my server with the app on it in the developers console. 我已使用cliendId,电子邮件地址和API密钥创建了服务帐户,该帐户与开发服务器控制台中的应用绑定到了我的服务器。 If someone can help me to understand how to create the key.p12 file, then I am pretty sure I can get this working. 如果有人可以帮助我了解如何创建key.p12文件,那么我很确定可以使它正常工作。

Thanks, 谢谢,

ski 滑雪

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

Go to https://cloud.google.com/console 前往https://cloud.google.com/console
Select your project 选择你的项目
Click on 'APIs and Auth' 点击“ API和验证”
Click on 'Credentials' 点击“凭据”
Click on 'Create new Client ID' 点击“创建新的客户ID”
Select 'Service Account' 选择“服务帐户”
Click 'Create Client ID' 点击“创建客户端ID”
Your private key will be downloaded. 您的私钥将被下载。
Your public key will be stored and displayed in the console. 您的公钥将被存储并显示在控制台中。
You will NOT be able to retrieve the private key again. 您将无法再次检索私钥。 Google only sends them once. Google只发送一次。 If you lose it, you must do the process over again, and update the relevant settings in the application. 如果丢失,则必须重新执行该过程,并更新应用程序中的相关设置。
If this question has been answered, please mark it as such. 如果已回答此问题,请在此标记。

请参阅google oauth2如何获取服务帐户的私钥 -这实际上已随新版本的Google Cloud Console发生了变化,但是答案是最新的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 服务帐户的oauth2 - oauth2 for service account 带有Google Speech API RPC服务帐户的Google Oauth2无法正常工作 - Google Oauth2 with Service account for Google Speech API rpc not working Nodejs 发布请求以获取服务帐户的 oauth2 令牌 - Nodejs post request to get oauth2 token for a service account 如何使用“服务帐户”的Google Oauth2凭据获取用户的电子邮件ID? - How can i get an users email id using Google Oauth2 Credentials of ‘Service Account’? 带有服务帐户的Google协调中心OAuth2 - Google Coordinate OAuth2 with Service Account 如何通过 OAuth2 在 GAS 中使用多个帐户? - How to use multiple account in GAS with OAuth2? 如何使用Javascript从Google Api检索服务帐户OAuth2令牌? - How can I retrieve a service account OAuth2 token from Google Api with Javascript? 带有服务帐户的Google OAuth2 - 指定要代表的用户电子邮件 - Google OAuth2 with Service Account - specifying the user email to act on behalf of Google API OAuth2,服务帐户,“错误”:“invalid_grant” - Google API OAuth2, Service Account, “error” : “invalid_grant” 如何使用hwioauthBundle在oauth2之后获取Google帐户电子邮件地址 - How to get google account email address after oauth2 using the hwioauthBundle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM