简体   繁体   English

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

[英]Access Google play account reports through Google API Client PHP Library

  • Google Play Developer Account reports are stored on private Google Cloud Storage bucket. Google Play开发者帐户报告存储在私有Google云端存储分区中。
  • I want to download these reports programmatically in PHP. 我想以PHP编程方式下载这些报告。
  • In this link section-> Download reports using a client library & service account provide steps for that. 在此链接部分 - > 使用客户端库和服务帐户下载报告,提供相应的步骤。
  • But I am not able to achieve it still. 但我仍然无法实现它。
  • Also no sample code is available for PHP, as Google API Client PHP Library are still in beta version. 此外,没有适用于PHP的示例代码,因为Google API客户端PHP库仍处于测试版。

So is it really possible to get access to private Google Cloud Storage bucket in PHP ? 那么是否真的可以通过PHP访问私有Google云存储桶?

I have already tried out with gsutil tool, but that doesn't exactly satisfy my need. 我已经尝试过使用gsutil工具,但这并不能完全满足我的需求。

Help will be appreciated. 帮助将不胜感激。

At first you must create an Application Account that you will use for authentication. 首先,您必须创建一个用于身份验证的应用程序帐户。 Go to Google Play Developer Console >> Settings >> API Access at the bottom of page click "Create Account" (not OAuth). 转到Google Play开发者控制台 >>设置>>页面底部的API访问权限单击“创建帐户”(不是OAuth)。 Then set permissions for created account and generate key file in JSON. 然后为已创建的帐户设置权限并在JSON中生成密钥文件。 Should look like this: 应该是这样的:

{
    "type": "service_account",
    "project_id": "api-...",
    "private_key_id": "...",
    "private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
    "client_email": "...@...iam.gserviceaccount.com",
    "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/..."
}

Next step use generated credentials in your PHP script. 下一步使用PHP脚本中生成的凭据。 Download or require in Composer Google Cloud Client Library . 在Composer Google Cloud Client Library中下载或要求。

Use Google Cloud Client Library methods to authorize and download report files: 使用Google Cloud Client Library方法授权和下载报告文件:

use Google\Cloud\Storage\StorageClient;

$client = new 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM