首先,您必須創建一個您將用於身份驗證的應用程序帳戶。轉到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
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();