繁体   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