【问题标题】:How to solve authentication error when accessing google pay api for passes?访问google pay api for pass时如何解决身份验证错误?
【发布时间】:2021-08-20 21:18:29
【问题描述】:

我一直按照herehere 的步骤尝试使用服务帐户密钥文件来访问 Google pay api 并插入 LoyaltyClass。 Google Pay 库建议使用 1.1.1 版的 Google api 客户端库,这就是我在这里所做的:

$client = new Google_Client();
$client->addScope('https://www.googleapis.com/auth/wallet_object.issuer');
$client->setApplicationName("Test");
$client->setDeveloperKey("My service account key");
$test_class = new LoyaltyClass;
$service = new Google_Service_Walletobjects($client);
$wallet_object = $test_class->create_wallet_class();
$service->loyaltyclass->insert($wallet_object);

我得到这个错误:

PHP Fatal error:  Uncaught Google_Service_Exception: Error calling POST https://www.googleapis.com/walletobjects/v1/loyaltyClass?key=2ee78c76d17cabcdce22c4e89af27ab73ad694a4: (401) Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. in /var/www/html/google-api-php-client/src/Google/Http/REST.php:76

云 api 控制台记录了我使用正确的服务帐户访问了 api,但没有记录任何请求。他们建议的链接指示我提示用户登录,但我想在没有用户输入的情况下访问这个 api,只是我的服务帐户。有没有办法做到这一点?

【问题讨论】:

    标签: php google-pay


    【解决方案1】:

    你要给setDeveloperKey提供什么?

    documentation 使用setAuthConfigFile 而不是setDeveloperKey

    $client = new Google_Client();
    $client->setApplicationName('Wallet Objects App');
    $client->setScopes(array('https://www.googleapis.com/auth/wallet_object.issuer'));
    $client->setAuthConfigFile('/example/path/to/privatekey.json');
    

    有一个 GitHub sample project 使用以下代码初始化 Google_Client 类:

    $this->client = new Google_Client();
    
    // do OAuth2.0 via service account file.
    // See https://developers.google.com/api-client-library/php/auth/service-accounts#authorizingrequests
    putenv('GOOGLE_APPLICATION_CREDENTIALS=' . SERVICE_ACCOUNT_FILE); // for Google_Client() initialization for server-to-server
    $this->client->useApplicationDefaultCredentials();
    // Set application name.
    $this->client->setApplicationName(APPLICATION_NAME);
    // Set Api scopes.
    $this->client->setScopes(array(SCOPES));
    

    SEVICE_ACCOUNT_FILEConfig.php 中定义。

    如果您想了解与 Passes API 集成的分步教程,您可以关注代码实验室:https://codelabs.developers.google.com/passes-loyaltyapi

    它是为 NodeJS 编写的,但大部分概念仍应适用于 PHP。

    编辑:更新为引用来自 Google Pay 开发者网站的 setAuthConfigFile

    编辑:更新以包含来自 GitHub 的 PHP 示例代码

    【讨论】:

    • 嘿,我尝试改用 setAuthConfig,但仍然遇到授权错误。当我查看 api 控制台中的凭据屏幕时,我可以单击服务帐户并转到密钥选项卡,它会显示与该帐户关联的密钥字符串。当我尝试使用 setAuthConfig 时,我使用了为我的服务帐户创建新密钥时自动下载的凭据。我应该使用其他凭据吗?非常感谢您的帮助!
    • 您能否确认setAuthConfig 已设置为您下载的文件?您下载的文件看起来与我在上面发布的 JSON 示例相似吗?您是否能够查看代码实验室以查看是否还有其他遗漏?例如granting access to the service account?
    • 另外,你能确认这是同一个授权错误吗?还是401 还是403
    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2015-01-24
    • 2021-03-19
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    • 2023-02-21
    相关资源
    最近更新 更多