如何解决请求在 Google my business for Review API 中缺少必需的身份验证凭据
我正在尝试集成 My business API
以获取我网站上的所有 google reviews
,即我的 google 位置。
我查看了几个答案,但无法找到问题所在。
我面临着如何获取访问令牌继续的问题。在设置 OAuth 同意步骤后,我通过了以下内容,这些内容是我从开发者控制台获得的。
'CLIENT_SECRET_PATH'=> 'client_secret_77331013078-oknip449r6oat5va0ef974d7nd7ncf1o.apps.googleusercontent.com.json'
'CREDENTIALS_PATH'=> 'credentials.json'
我的代码:
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setAuthConfigFile(CLIENT_SECRET_PATH);
$client->addScope("https://www.googleapis.com/auth/plus.business.manage");
// https://www.googleapis.com/auth/business.manage
$client->setRedirectUri($redirect_uri);
// For retrieving the refresh token
$client->setAccesstype("offline");
$client->setApprovalPrompt("auto");
/************************************************
We are going to create the Google My Business API
service,and query it.
************************************************/
$mybusinessService = new Google_Service_Mybusiness($client);
$credentialsPath = CLIENT_SECRET_PATH;
if (isset($_GET['code'])) {
// Exchange authorization code for an access token.
$accesstoken = $client->authenticate($_GET['code']);
// Store the credentials to disk.
if (!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath),0700,true);
}
file_put_contents($credentialsPath,$accesstoken);
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect,FILTER_SANITIZE_URL));
}
// Load prevIoUsly authorized credentials from a file.
if (file_exists($credentialsPath)) {
$accesstoken = file_get_contents($credentialsPath);
$client->setAccesstoken($accesstoken);
// // Refresh the token if it's expired.
if ($client->isAccesstokenExpired()) {
$client->refreshToken($client->getRefreshToken());
file_put_contents($credentialsPath,$client->getAccesstoken());
}
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
}
Error 1: Uncaught invalidargumentexception: Invalid token format=> Google\Client->setAccesstoken(Array) #1 {main} thrown
Error 2: {
"error":{
"code":401,"message":"Request is missing required authentication credential. Expected OAuth 2 access token,login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.","errors":[
{
"message":"Login required.","domain":"global","reason":"required","location":"Authorization","locationType":"header"
}
],"status":"UNAUTHENTICATED"
}
}
我对第一个 Error1
的理解是 Access token
不是正确的格式。它从 JSON 格式的 client****.json 文件中获取内容。我该如何解决?以便它创建一个访问令牌?
解决方法
您的错误可能源于 CLIENT_SECRET_PATH
应该是您从 Google 开发者控制台下载的credentials.json 文件的路径。不仅仅是客户 ID。
确保您创建了网络浏览器凭据。
您的代码如何工作
听起来您并不完全了解您的代码是如何工作的。这是您第一次运行时会发生的情况。
它会打到这个部分
$authUrl = $client->createAuthUrl();
这将打开授权窗口,也可能是一个谷歌登录窗口。一旦发生这种情况,它将重新路由到您的代码并点击
if (isset($_GET['code'])) {
因为它将在查询参数中返回一个代码(授权代码)。
那时因为
if (!file_exists(dirname($credentialsPath))) {
不存在,届时将为您创建一个新文件。
下次您的代码运行时,它会生成一个文件,因此以下内容为真
if (file_exists($credentialsPath)) {
因此它将使用文件中的代码为您获取一个新的访问令牌。
注意:如所写,您的代码将为单用户。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。