簡體   English   中英

如何使用 OAuth 協議連接到 Google My Business API?

[英]How to connect to Google My Business API using OAuth protocol?

我對在 PHP 中使用 Google API 很陌生,我目前正在嘗試獲取為企業提交的最后一次 Google 評論的列表。

一開始,我使用 Google Places API 來完成這項工作,使用起來很簡單(不需要 OAuth 連接過程,一個簡單的 HTTP 請求就足夠了)但我了解到這種方法只返回最后 5 條評論生意,我需要得到一份所有評論的清單。

這就是為什么我認為如果我想獲得一個企業的完整評論列表,我需要使用 Google My Business API。 但是已經有幾天我試圖讓這個 API 工作,我仍然沒有設法使用 OAuth 協議將我的應用程序連接到 API。

雖然我是從一個預制的 Github 示例開始的,但在嘗試訪問 API 時我仍然收到此錯誤消息:我目前收到的 PHP 錯誤消息

我不明白為什么,因為我真的做了文檔中提到的所有事情。

如果有人可以稍微看看我的代碼並幫助我了解它的問題,那將是巨大的幫助! 任何信息將不勝感激。

這是我的代碼:

$credentials = get_stylesheet_directory() . '/allia-google-api/google-my-business-secrets.json';

$client = new Google\Client();
$client->setAuthConfig($credentials);
$client->addScope("https://www.googleapis.com/auth/business.manage");
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$client->setRedirectUri($redirect_uri);
$my_business_account = new Google_Service_MyBusinessAccountManagement($client);

$list_accounts_response = $my_business_account->accounts->listAccounts();
var_dump($list_accounts_response);

從錯誤中我可以看到你有驗證錯誤,請確保您的CLIENT_SECRETS這應該是一個JSON文件是正確的。 下面的代碼將列出所有帳戶,它是使用名為帳戶管理API新的API。 谷歌我的業務API將得到很快過時。

<?php 

include_once __DIR__ . '/vendor/autoload.php';


$credentials = __DIR__ . '/client_secrets.json';

$client = new Google\Client();
$client->setAuthConfig($credentials);
$client->addScope("https://www.googleapis.com/auth/business.manage");
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$client->setRedirectUri($redirect_uri);
$my_business_account = new Google_Service_MyBusinessAccountManagement($client);

if (isset($_GET['logout'])) { // logout: destroy token
    unset($_SESSION['token']);
  die('Logged out.');
}

if (isset($_GET['code'])) { // get auth code, get the token and store it in session
    $client->authenticate($_GET['code']);
    $_SESSION['token'] = $client->getAccessToken();
}


if (isset($_SESSION['token'])) { // get token and configure client
    $token = $_SESSION['token'];
    $client->setAccessToken($token);
}

if (!$client->getAccessToken()) { // auth call 
    $authUrl = $client->createAuthUrl();
    header("Location: ".$authUrl);
    die;
}

$list_accounts_response = $my_business_account->accounts->listAccounts();
Var_dump($list_accounts_response);

?>

在這里查看 [https://github.com/google/google-my-business-samples/tree/master/php]

非常感謝您的回答,我之前已經嘗試過這種方法,是的,我的憑據 json 文件是正確的。

問題是我也不知道 $_GET['code'] 指的是什么......

因此我無法驗證我的客戶端,我總是重定向到 Google 驗證錯誤頁面。

你知道這個 $_GET['code'] 應該是什么嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM