gpt4 book ai didi

google-api-php-client - 刷新 OAuth2 token 时出错 { "error": "invalid_grant" }

转载 作者:行者123 更新时间:2023-12-04 22:15:36 24 4
gpt4 key购买 nike

请帮助我了解错误的地方。

API

Google Api PHP v3,key.p12 是 .zip 集合 API 中的文件(或者我在哪里找到它?)

我用 xxxxxxxxxx 屏蔽了我的数据

对不起我的英语不好

gaphp.php

<?php

require_once 'Google/Client.php';
require_once 'Google/Service/Analytics.php';

$client_id = 'xxx-xxx.apps.googleusercontent.com';
$service_account_name = 'xxx-xxx@developer.gserviceaccount.com';
$keyfile = 'key.p12';
$redirect_url = 'http://example.com/service/ga/gaphp.php';
$client_secret = 'xxxxxxxxxxxxxxxxxxxxx';

// Initialise the Google Client object
$client = new Google_Client();
$client->setApplicationName('My application');
$client->setRedirectUri($redirect_url);
$client->setClientSecret($client_secret);

$client->setAssertionCredentials(
new Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/analytics'),
file_get_contents($keyfile)
)
);

// Get this from the Google Console, API Access page
$client->setClientId($client_id);
$client->setAccessType('offline_access');
$analytics = new Google_Service_Analytics($client);

// We have finished setting up the connection,
// now get some data and output the number of visits this week.

// Your analytics profile id. (Admin -> Profile Settings -> Profile ID)
$analytics_id = 'ga:xxxxxx';
$lastWeek = date('Y-m-d', strtotime('-1 week'));
$today = date('Y-m-d');

try {
$results = $analytics->data_ga->get($analytics_id, $lastWeek, $today,'ga:visits');
echo '<b>Number of visits this week:</b> ';
echo $results['totalsForAllResults']['ga:visits'];
} catch(Exception $e) {
echo 'There was an error : - ' . $e->getMessage();
}

?>

错误

There was an error : - Error refreshing the OAuth2 token, message: 'Protection: 1; mode=block Server: GSE Alternate-Protocol: 443:quic Transfer-Encoding: chunked { "error" : "invalid_grant" }'

最佳答案

我昨天刚刚使用服务帐户进行了分析 - 所以我也许可以提供帮助。从控制台,https://console.developers.google.com/project ,您可以选择您的项目并转到“APIS & AUTH”。确保分析 API 已打开。并确保您拥有服务帐户“凭据”。确保为您的谷歌分析属性授予服务帐户电子邮件地址权限。您还需要为您的服务帐户获取 P12 key 文件,并在您的服务器上提供该 key 文件。实际上,我为我的 $key_file_location 变量使用了完整的文件路径。

我在 GitHub 上使用了这段代码:https://github.com/google/google-api-php-client/blob/master/examples/service-account.php

该示例适用于 Books API - 因此只需将“Books”替换为“Analytics”,将“books”替换为“analytics”。填写您的服务帐户凭据,并记住将您的服务帐户电子邮件添加到您的谷歌分析属性。

关于google-api-php-client - 刷新 OAuth2 token 时出错 { "error": "invalid_grant" },我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23590164/

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