[英]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.