请求在 Google my business for Review API 中缺少必需的身份验证凭据

时间:2021-03-27 09:38:37

标签: php google-api google-oauth google-api-php-client google-my-business-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 文件中获取内容。我该如何解决?以便它创建一个访问令牌?

另外还需要做什么才能解决这两个错误?请指导我。

1 个答案:

答案 0 :(得分:2)

您的错误可能源于 CLIENT_SECRET_PATH 应该是您从 Google 开发者控制台下载的credentials.json 文件的路径。不仅仅是客户 ID。

确保您创建了网络浏览器凭据。

您的代码如何工作

听起来您并不完全了解您的代码是如何工作的。这是您第一次运行时会发生的情况。

它会打到这个部分

$authUrl = $client->createAuthUrl();

这将打开授权窗口,也可能是一个谷歌登录窗口。一旦发生这种情况,它将重新路由到您的代码并点击

if (isset($_GET['code'])) {

因为它将在查询参数中返回一个代码(授权代码)。

那时因为

if (!file_exists(dirname($credentialsPath))) {

不存在,届时将为您创建一个新文件。

下次您的代码运行时,它会生成一个文件,因此以下内容为真

if (file_exists($credentialsPath)) {

因此它将使用文件中的代码为您获取一个新的访问令牌。

注意:如所写,您的代码将为单用户。