まず、認証に使用するアプリケーションアカウントを作成する必要があります。 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スクリプトで使用します。作曲家でダウンロードまたはご依頼くださいGoogle Cloud Client Library。
レポートファイルを承認およびダウンロードする
利用のGoogle Cloudクライアントライブラリのメソッド:
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();