Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 请求缺少Google my Business for Review API中所需的身份验证凭据_Php_Google Api_Google Oauth_Google Api Php Client_Google My Business Api - Fatal编程技术网

Php 请求缺少Google my Business for Review API中所需的身份验证凭据

Php 请求缺少Google my Business for Review API中所需的身份验证凭据,php,google-api,google-oauth,google-api-php-client,google-my-business-api,Php,Google Api,Google Oauth,Google Api Php Client,Google My Business Api,我正在尝试集成我的业务API,以获取我网站上的所有谷歌评论,以及我的谷歌位置。 我已经看了几个答案,但没有找到问题所在。 我面临如何获取访问令牌以继续的问题。在设置OAuth同意步骤之后,我已经通过了从开发人员控制台获得的以下内容 'CLIENT_SECRET_PATH'=> 'client_secret_77331013078-oknip449r6oat5va0ef974d7nd7ncf1o.apps.googleusercontent.com.json' 'CREDENTIALS_PA

我正在尝试集成
我的业务API
,以获取我网站上的所有
谷歌评论
,以及我的谷歌位置。 我已经看了几个答案,但没有找到问题所在。 我面临如何获取访问令牌以继续的问题。在设置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文件中获取内容,该文件采用json格式。我怎样才能修好它?这样它就可以创建一个访问令牌了


另外,为了解决这两个错误,还需要做些什么?请告诉我。

您的错误可能源于这样一个事实:
CLIENT\u SECRET\u PATH
应该是您从Google开发者控制台下载的credentials.json文件的路径。不仅仅是客户id

确保已创建web浏览器凭据

你的代码是如何工作的 听起来好像你并不完全理解你的代码是如何工作的。下面是第一次运行时将发生的情况

它将袭击这个区域

$authUrl = $client->createAuthUrl();
这将打开授权窗口,可能还会打开谷歌登录窗口。一旦发生这种情况,它将重新路由到您的代码并点击

if (isset($_GET['code'])) {
因为它将在查询参数中返回一个代码(授权代码)

在那一点上,因为

if (!file_exists(dirname($credentialsPath))) {
不存在届时将为您创建一个新文件

下一次代码运行时,它将有一个文件,因此以下内容将是正确的

if (file_exists($credentialsPath)) {
因此,它将使用文件中的代码为您获取一个新的访问令牌

注意:在编写时,您的代码将是单用户的