【问题标题】:Access Google play account reports through Google API Client PHP Library通过 Google API 客户端 PHP 库访问 Google Play 帐户报告
【发布时间】:2016-05-25 09:38:44
【问题描述】:
  • Google Play 开发者帐户报告存储在私有 Google Cloud Storage 存储桶中。
  • 我想用 PHP 以编程方式下载这些报告。
  • 在此link 部分中-> 使用客户端库和服务帐户下载报告提供了相关步骤。
  • 但我仍然无法实现。
  • 也没有可用于 PHP 的示例代码,因为 Google API 客户端 PHP 库仍处于测试版。

那么真的有可能在 PHP 中访问私有的 Google Cloud Storage 存储桶吗?

我已经尝试过使用 gsutil 工具,但这并不能完全满足我的需求。

我们将不胜感激。

【问题讨论】:

    标签: php google-cloud-storage


    【解决方案1】:

    首先,您必须创建一个用于身份验证的应用程序帐户。转到页面底部的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": "...@...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/..."
    }
    

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

    使用 Google Cloud 客户端库方法授权和下载报告文件:

    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();
    

    【讨论】:

    • 对于未来的开发者:@icoder 解决方案是正确的。当您创建服务帐户并授予其权限时,您必须等待几个小时(我是 24 小时)才能使用您的服务帐户下载报告。我浪费了一周的时间来创建/删除服务帐户,但您必须耐心等待
    • 如果我正确理解了提供的链接 (support.google.com/googleplay/android-developer/answer/…),则服务帐户是在不同的谷歌云项目中创建的,而不是在 Google Play Android 开发者项目中创建的。 @Stefano,您能确认这是您所做的吗?您是在另一个项目(通过谷歌云控制台)中创建服务帐户,还是实际上在 Google Play 控制台的 API 访问区域中创建了服务帐户?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 2021-09-05
    • 2016-12-03
    • 1970-01-01
    • 2017-10-18
    相关资源
    最近更新 更多