php - 谷歌网站站长工具 API

标签 php google-api-php-client google-search-console

我正在尝试提取我公司已在其 Google 网站管理员工具中注册的公司列表。我正在使用 PHP 和适用于 PHP 的 Google API 客户端库 (https://developers.google.com/api-client-library/php/) 执行此操作。我已经把一切都搞定了,除了结果是它给了我一个空数组。我的代码在下面,任何帮助将不胜感激。

为客户端 ID、帐户等填写了适当的值,并且所有内容都进行了适当的身份验证,因为我使用相同的代码来访问图书 API。

    <?php
    /*
     * Copyright 2013 Google Inc.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    session_start();
    include_once "templates/base.php";

    /************************************************
      Make an API request authenticated with a service
      account.
     ************************************************/
    require_once realpath(dirname(__FILE__) . '/../autoload.php');

    /************************************************
      ATTENTION: Fill in these values! You can get
      them by creating a new Service Account in the
      API console. Be sure to store the key file
      somewhere you can get to it - though in real
      operations you'd want to make sure it wasn't
      accessible from the webserver!
      The name is the email address value provided
      as part of the service account (not your
      address!)
      Make sure the Books API is enabled on this
      account as well, or the call will fail.
     ************************************************/
    $client_id = <Client ID>; //Client ID
    $service_account_name = <Service Account Name>; //Email Address
    $key_file_location = <Key Location>; //key.p12

    echo pageHeader("Service Account Access");
    if ($client_id == <Client ID>
        || !strlen($service_account_name)
        || !strlen($key_file_location)) {
      echo missingServiceAccountDetailsWarning();
    }

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

    /************************************************
      If we have an access token, we can carry on.
      Otherwise, we'll get one with the help of an
      assertion credential. In other examples the list
      of scopes was managed by the Client, but here
      we have to list them manually. We also supply
      the service account
     ************************************************/
    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'),
        $key
    );
    $client->setAssertionCredentials($cred);
    if ($client->getAuth()->isAccessTokenExpired()) {
      $client->getAuth()->refreshTokenWithAssertion($cred);
    }
    $_SESSION['service_token'] = $client->getAccessToken();

    /************************************************
      We're just going to make the same call as in the
      simple query as an example.
     ************************************************/

    $results = $service->sites->listSites();

    $siteList = $results->siteEntry;

var_dump($siteList);

// $optParams = array('filter' => 'free-ebooks');
// $results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
// echo "<h3>Results Of Call:</h3>";
// foreach ($results as $item) {
//   echo $item['volumeInfo']['title'], "<br /> \n";
// }

echo pageFooter(__FILE__);

最佳答案

我想通了。这是因为我没有在网站管理员工具界面中为我尝试连接的用户分配适当的权限。只需点击每个站点旁边的齿轮并添加用户即可。

关于php - 谷歌网站站长工具 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28436439/

相关文章:

javascript内联如果变量赋值像php

php - 使用 php 的 Drive API 中的凭据无效

PHP:调用 Google Play Developer API 中未定义的方法

indexing - 软404出现在空的内部搜索结果中,怎么办?

php - 在 Facebook 应用程序中使用信用卡

javascript - jquery更改php if语句上的元素

url - 如何改进 URL 的结构

push-notification - 无法将我自己的域添加到 google api 通知端点

php - 将 MySQL 结果转换为 JSON

google-api - 如何使用 GuzzleHttp\Psr7\Response 对象下载文件?