如何使用Google网站管理员工具API访问用户数据

Miquel Correa卡萨布兰卡

我刚刚开始使用Google的api库,但是在访问用户数据时遇到了一些麻烦。为了进行实验,我使用了该领域中来自previos问题的代码。我的代码对google搜索控制台api执行了请愿,但返回了一个空数组。我通过管理控制台(https://developers.google.com/identity/protocols/OAuth2ServiceAccount对服务帐户进行了授权,因此我不知道为什么它不返回任何内容。

set_include_path(get_include_path() . PATH_SEPARATOR . '$path/google-api-php-client-master/src');
require_once realpath('$path/google-api-php-client-master/src/Google/autoload.php');

$client_id = "my-client-id"; 
$service_account_name = "[email protected]"; 
$key_file_location = "privatekey.p12";

$client = new Google_Client();
$client->setApplicationName("WMtools_application");
$service = new Google_Service_Webmasters($client);

if (isset($_SESSION['service_token'])) {
    $client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
    $service_account_name,
    array('https://www.googleapis.com/auth/webmasters.readonly'),
    $key
);
try{
    $client->setAssertionCredentials($cred);
    if ($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion($cred);
  }
    $_SESSION['service_token'] = $client->getAccessToken();
}catch(Exception $e){
    echo "Exception captured",  $e->getMessage(), "\n";
}
try{
    $results = $service->sites->listSites();

    $siteList = $results->siteEntry;

    var_dump($siteList);
}catch(Exception $e2){
    echo "No results",  $e2->getMessage(), "\n";
}
Miquel Correa卡萨布兰卡

我解决了问题。情况是,我已经将域范围的权限委派给了我的服务帐户,并且在我向API提交的请愿书中没有包括我要从中访问数据的用户电子邮件。要解决它,只需将它以这种方式添加到google_Auth_AssertionCredentials中:

$cred = new Google_Auth_AssertionCredentials(
    $service_account_name,
    array('https://www.googleapis.com/auth/webmasters.readonly'),
    $key
);
$cred->sub = $service_account_user_email; //[email protected]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Google网站管理员工具API

如何使用PHP以编程方式在Google网站管理员工具中提交站点地图?

如何在没有管理员凭据的情况下访问CouchDB用户数据库

Google API 离线访问用户数据

限制管理员访问 Azure Ad B2C 用户数据

如何管理用户数据

我能否使用同一组织管理员的 Salesforce 访问令牌访问所有用户数据

提取为Google网站管理员工具

Google Analytics网站管理员工具权限不足

Google网站管理员工具和重复的链接

查询网站管理员工具API的维度

Google网站管理员工具API-无法获得我拥有的域的权限

如何从 Google 网站管理员工具中删除旧的子域 URL?

在 gitlab ce 中使用 LetsEncrypt 和 Google 网站管理员工具

如何从网站管理员工具api获取查询详细信息

使用 PHP 从多个网站跟踪用户数据

Flutter FirebaseUser 如何访问用户数据

使用收到的访问令牌检索Keycloak用户数据

如何使用Spring事务管理来更新当前用户数据库?

使用 Google API 检索与我的 API 相关的用户数据

网站管理员工具无法检测到我网站上的结构化数据

用户数据的Google API URL是什么?

Google网站管理员工具不会为我的网站编制索引

Google网站管理员工具说我的XML网站地图“似乎是HTML页面”

Firebase python,访问用户数据

如何分别显示总注册管理员或用户数?

Google Adword&Search Console(Google网站管理员工具)

使用Twitter API登录后如何获取用户数据

如何从 API 获取用户数据以在 React 应用程序中使用?