通过Google API客户端访问Google Play帐户报告PHP库

  •   
  • 1425
  • PHP
  • 0
  • super_dodo
  • 2021/09/07

Google Play开发者帐户报告存储在私有Google云端存储分区中。
我想用PHP编程下载这些报告。
在此link部分 - >使用客户端库下载报告&服务帐户为此提供了步骤。
但我仍然无法实现它。
也没有示例代码可用于PHP,因为Google API客户端PHP库仍处于测试版。
那么真的有可能在PHP中访问私有Google Cloud Storage存储桶吗?通过Google API客户端访问Google Play帐户报告PHP库.

首先,您必须创建一个您将用于身份验证的应用程序帐户。转到Google Play Developer Console >>设置>> API访问页面底部点击“创建帐户”(而不是OAuth)。然后为创建的帐户设置权限并以JSON生成密钥文件。应该看起来像这样:

{ 
    "type": "service_account", 
    "project_id": "api-...", 
    "private_key_id": "...", 
    "private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n", 
    "client_email": "[email protected]", 
    "client_id": "...", 
    "auth_uri": "https://accounts.google.com/o/oauth2/auth", 
    "token_uri": "https://accounts.google.com/o/oauth2/token", 
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", 
    "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/..." 
}

下一步在您的PHP脚本中使用生成的凭据。下载或需要在Composer Google Cloud Client Library。

使用谷歌云客户端库的方法来授权和下载报告文件:

use Google\Cloud\Storage\StorageClient; 

$client = StorageClient([ 
    'scopes' => [StorageClient::READ_ONLY_SCOPE], 
    'keyFile' => json_decode(file_get_contents('yourKeyFile.json'), true) 
]); 
$bucket = $client->bucket('pubsite_prod_rev_*'); //Find your bucket name in Google Developer Console >> Reports 

//List all objects in bucket 
//$prefix = "earnings/earnings_{$year}{$month}_";
foreach ($bucket->objects(['prefix' => 'stats/installs/']) as $object) { 
    print_r($object->name()); 
} 

//Download some file 
$file = $bucket->object('stats/installs/installs_*_overview.csv'); 
$file->downloadToFile(); 

//Or print as string 
echo $file->downloadAsString();

参考网址:

https://support.google.com/googleplay/android-developer/answer/6135870
http://cn.voidcc.com/question/p-ehinpzyp-bma.html

https://support.google.com/googleplay/android-developer/answer/9844686
https://stackoverflow.com/questions/66102314/can%C2%B4t-access-google-cloud-storage-with-service-account-service-account-does-not

亲测有效。注意相关的权限设置。