【问题标题】:How to get OAuth2 working with a service account如何让 OAuth2 使用服务帐户
【发布时间】:2014-01-07 07:22:58
【问题描述】:

我们需要从旧的配置 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, 数组('https://www.googleapis.com/auth/prediction'), $密钥) );

【问题讨论】:

    标签: google-api oauth-2.0


    【解决方案1】:

    转到https://cloud.google.com/console
    选择您的项目
    点击“API 和身份验证”
    点击“凭据”
    点击“创建新的客户 ID”
    选择“服务帐户”
    点击“创建客户 ID”
    您的私钥将被下载。
    您的公钥将存储并显示在控制台中。
    您将无法再次检索私钥。谷歌只发送一次。如果丢失,则必须重新执行该过程,并更新应用程序中的相关设置。
    如果此问题已回答,请注明。

    【讨论】:

      【解决方案2】:

      请参阅google oauth2 how to get private key for service account - 今天的新版 Google Cloud Console 确实改变了这一点,但答案是最新的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-19
        • 2014-09-22
        • 2020-05-31
        • 2017-01-23
        • 2019-01-23
        • 2013-08-21
        • 2013-05-27
        • 1970-01-01
        相关资源
        最近更新 更多