首页 > 解决方案 > 如何从 Google Cloud 获取 p12 文件(key.p12)?

问题描述

我需要从https://console.cloud.google.com/下载我的 key.p12 文件,但我现在不知道该怎么做。我的仪表板“Api & Services > Credentials”是这样的:

控制台云谷歌仪表板

我需要这个 p12 文件以使用此代码连接到 PHP API Google_Service_AndroidPublisher(我在此 Stackoverflow 对问题Get android subscription status, failed with 403的回答中使用相同的代码:

$service_account_name = 'testing@nootrictesting.iam.gserviceaccount.com'; //Your service account email
$key_file_location = ''; // p12 file (key.p12)

$client = new Google_Client();
$client->setApplicationName("My name app"); //This is the name of the linked application

$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
    $service_account_name,
    array('https://www.googleapis.com/auth/androidpublisher'),
    $key
);

$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion($cred);
}
$apiKey = ""; //API key
$client->setDeveloperKey($apiKey);

$service = new Google_Service_AndroidPublisher($client);
$results = $service->purchases_subscriptions->get("MY_ANDROID_APP_PACKAGE", $product_id, $purchase_token, array());

任何帮助都会很有用。谢谢!!!

标签: androidgoogle-apisubscriptionin-app

解决方案


DalmoTo 在他们的评论中链接了视频,该视频显示了如何/在哪里获取钥匙:youtu.be/asrCdWFrF0A?t=76

但是,Google_Auth_AssertionCredentials 看起来不应该再使用了(https://github.com/googleapis/google-api-php-client/blob/master/UPGRADING.md)。相反,使用$client->setAuthConfig('/path/to/service-account.json'). 然后你可以使用 json 密钥文件,而不是 p12 密钥文件。


推荐阅读