gpt4 book ai didi

php - 从 Google_Service_AnalyticsReporting (google/apiclient SDK) 切换到 BetaAnalyticsDataClient (google/analytics-data SDK)

转载 作者:行者123 更新时间:2023-12-03 08:15:34 24 4
gpt4 key购买 nike

我正在将 Google Analytics(分析)媒体资源从 UA 切换到 GA4,并尝试使用数据 API 提取 GA4 媒体资源的 Google Analytics(分析)数据。我目前正在使用 google/apiclient PHP SDK 来处理 UA 属性,但需要切换到 google/analytics-data SDK 来处理 GA4 属性。我能找到的所有凭据文档都是为使用服务帐户而编写的。不幸的是,我们当前的设置无法使用服务帐户,需要以用户 [email protected] 身份登录。这是已添加到我们帐户内的资源中的电子邮件/用户。

对于 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": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3e505f535b7e5347134e4c51545b5d4a13505f535b10575f5310594d5b4c48575d5b5f5d5d514b504a105d5153" rel="noreferrer noopener nofollow">[email protected]</a>",
"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 详细信息进行连接?

最佳答案

我设法在不指定范围的情况下做同样的事情。我认为这是没有必要的,因为它没有写在文档中,而且它对我来说工作得很好。我只使用了我的 json 文件,它是我的服务器端 key 。

$config = [
'credentials' => "/PATH/TO/JSON/FILE",
];

$client = new BetaAnalyticsDataClient($config);

关于php - 从 Google_Service_AnalyticsReporting (google/apiclient SDK) 切换到 BetaAnalyticsDataClient (google/analytics-data SDK),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69544249/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com