1

我正在将 Google Analytics 属性从 UA 切换到 GA4,并尝试使用 Data API 为 GA4 属性提取 Google Analytics 数据。我目前正在将 google/apiclient PHP SDK 用于 UA 属性,但需要切换到 google/analytics-data SDK 用于 GA4 属性。我可以找到的所有凭据文档都是为使用服务帐户编写的。不幸的是,我们目前的设置不能使用服务帐户,需要以用户analytics@company.com 登录,这是我们帐户内属性中添加的电子邮件/用户。

对于 google/apiclient SDK,我们像这样进行连接:

$appName = 'My Analytics App';
// Decoded from json file for example
$clientAuthFileData = [
    'web' => [
        'client_id'                   => '######-#######.apps.googleusercontent.com',
        'project_id'                  => 'my-project-name',
        'auth_uri'                    => 'https://accounts.google.com/o/oauth2/auth',
        'token_uri'                   => 'https://oauth2.googleapis.com/token',
        'auth_provider_x509_cert_url' => 'https://www.googleapis.com/oauth2/v1/certs',
        'client_secret'               => 'XXXXXXXXXXX',
        'redirect_uris'               =>
            [
                'https://site1.com/authorize.php',
                'https://site2.com/authorize.php',
                'https://site3.com/authorize.php',
                'https://site4.com/authorize.php',
                'https://site5.com/authorize.php',
            ],
    ],
];
// Decoded from json file for example
$authFileData       = [
    'access_token'  => 'XXXXXXXX',
    'expires_in'    => 3600,
    'refresh_token' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
    'scope'         => 'https://www.googleapis.com/auth/userinfo.email openid https://www.googleapis.com/auth/analytics.readonly',
    'token_type'    => 'Bearer',
    'id_token'      => 'XXXXXXXXXXXXXXXXXXXXXX.XXXXXXXXXXXXXXXXXXX',
    'created'       => 1553570124,
];

$client = new Google_Client();
$client->setApplicationName( $appName );
$client->setAccessType( "offline" );
$client->setAuthConfig( $clientAuthFileData );
$client->fetchAccessTokenWithRefreshToken( $authFileData['refresh_token'] );

$client = new Google_Service_AnalyticsReporting( $client );

要使用 google/analytics-data SDK 中的服务帐户执行相同的调用,它看起来像这样:

// Decoded from json file for example
$serviceAccountCredentials = '{
  "type": "service_account",
  "project_id": "my-project-name",
  "private_key_id": "XXXXXXXXXX",
  "private_key": "-----BEGIN PRIVATE KEY-----XXXXXXXXX\n-----END PRIVATE KEY-----\n",
  "client_email": "name@my-project-name.iam.gserviceaccount.com",
  "client_id": "XXXXXXXXXX",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/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/name%40my-project-name.iam.gserviceaccount.com"
}';

$client = new BetaAnalyticsDataClient( [
    'credentials' => json_decode( $serviceAccountCredentials, true ),
] );

对于 GA4,我尝试了以下方法:

/*
THROWS
Fatal error: Uncaught InvalidArgumentException: json key is missing the type field in /var/www/html/vendor/google/auth/src/CredentialsLoader.php on line 150

InvalidArgumentException: json key is missing the type field in /var/www/html/vendor/google/auth/src/CredentialsLoader.php on line 150
*/
$client = new BetaAnalyticsDataClient( [
    'credentials' => $clientAuthFileData,
] );

/*
THROWS
Fatal error: Uncaught DomainException: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information in /var/www/html/vendor/google/gax/src/CredentialsWrapper.php on line 267
Google\ApiCore\ValidationException: Could not construct ApplicationDefaultCredentials in /var/www/html/vendor/google/gax/src/CredentialsWrapper.php on line 267
 */
$client = new BetaAnalyticsDataClient( [
    'keyFile' => $clientAuthFileData,
] );

如何使用与 google/apiclient SDK 相同的 OAuth 详细信息进行连接?

4

1 回答 1

1

通过挖掘类文件来了解它们如何处理凭据。对于遇到此问题的其他任何人,您可以使用:

$client = new BetaAnalyticsDataClient( [
    'credentials' => Google\ApiCore\CredentialsWrapper::build( [
        'scopes'  => [
            'https://www.googleapis.com/auth/analytics',
            'openid',
            'https://www.googleapis.com/auth/analytics.readonly',
        ],
        'keyFile' => [
            'type'          => 'authorized_user',
            'client_id'     => $clientAuthFileData['web']['client_id'],
            'client_secret' => $clientAuthFileData['web']['client_secret'],
            'refresh_token' => $authFileData['refresh_token'],
        ],
    ] ),
] );

我在这里将范围设置为一个数组,因为这是类所期望的方式,并且在 JSON 文件中它们是一个用空格分隔的字符串,您可能可以使用以下内容,但我没有对其进行测试:

$client = new BetaAnalyticsDataClient( [
    'credentials' => Google\ApiCore\CredentialsWrapper::build( [
        'scopes'  => explode( ' ', $authFileData['scope'] ),
        'keyFile' => [
            'type'          => 'authorized_user',
            'client_id'     => $clientAuthFileData['web']['client_id'],
            'client_secret' => $clientAuthFileData['web']['client_secret'],
            'refresh_token' => $authFileData['refresh_token'],
        ],
    ] ),
] );
于 2021-10-12T21:25:33.043 回答