Google Webmasters API for Java返回空站点列表 [英] Google Webmasters API for Java returns empty site list

查看:188
本文介绍了Google Webmasters API for Java返回空站点列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个简单的网站列表查询代码,该代码根据Google的服务帐户 = noreferrer>文档。正在使用的身份验证密钥文件(.p12)和帐户都有效。

I have written a simple site list query code which uses Oauth with service account based on Google's documentation. The authentication key file (.p12) being used is valid as well as the account.

问题是sites list方法返回一个空列表。

The problem is that sites list method returns an empty list.

service.sites().list().execute();

此外,如果我明确尝试获取经过验证的网站的站点地图,请致电

Also if I explicitly try to get sitemaps of a validated site, by calling

service.sitemaps().list("my.sample.site.com").execute();

我收到了403 Forbidden - 用户没有足够的网站权限'sample.site.com '。另请参阅: https://support.google.com/webmasters/answer/2451999。来自API的错误。

I got a 403 Forbidden - "User does not have sufficient permission for site 'sample.site.com'. See also: https://support.google.com/webmasters/answer/2451999." error from the API.

根据我的调试,API完美地加载密钥文件(.p12)并管理访问令牌等,没有任何问题。

According to my debugging, the API perfectly loads the key file (.p12) and manages the access tokens etc. with no problems.

尽管我的服务帐户身份验证可能存在问题。

Nevertheless there might be a problem with my service account authentication.

依赖关系:

<dependency>
  <groupId>com.google.apis</groupId>
  <artifactId>google-api-services-webmasters</artifactId>
  <version>v3-rev6-1.20.0</version>
</dependency>

示例代码:

package webmastertools;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.webmasters.Webmasters;
import com.google.api.services.webmasters.WebmastersScopes;
import com.google.api.services.webmasters.model.SitesListResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.util.Collections;

public class GoogleWebmastersClient {

    static Log logger = LogFactory.getLog(GoogleWebmastersClient.class);

    public static void main(String args[]) {

        try {

            HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
            JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
            String emailAddress = "012345789@developer.gserviceaccount.com";
            String applicationName = "Sitemap Generator";

            GoogleCredential credential = new GoogleCredential.Builder()
                    .setTransport(httpTransport)
                    .setJsonFactory(jsonFactory)
                    .setServiceAccountId(emailAddress)
                    .setServiceAccountPrivateKeyFromP12File(new File("/path/to/auth.p12"))
                    .setServiceAccountScopes(Collections.singletonList(WebmastersScopes.WEBMASTERS))
                    .build();

            Webmasters service = new Webmasters.Builder(httpTransport, jsonFactory, credential)
                    .setApplicationName(applicationName)
                    .build();

            SitesListResponse siteList = service.sites().list().execute();

            if (siteList.isEmpty()) {
                logger.info("Site list is empty!");
            }

        } catch (Exception e) {
            logger.error("Exception: ", e);
        }
    }
}

这有什么不对吗代码?

推荐答案

在对Google的Oauth身份验证代码进行一些调试后,我的一位同事发现我的代码没有任何问题但是Google网站管理员控制台应该遵循一定的程序,顺便提一下:

After some debugging of Google's Oauth authentication code, a colleague of mine discovered that there was nothing wrong with my code but there is a certain procedure that should be followed at Google Webmasters Console which nowhere to mentioned by the way:


  • 你必须给予对您的每个域的服务帐户电子邮件地址的完全权限。如果您之前提供了所有权,则必须撤销该所有权(因为如果您在授予权限之前授予所有权,则无法修改权限。)

  • You have to give "full" permission to your service account email address for each of your domains. If you gave the "ownership" before, you have to revoke that ownership (because you can not modify the permission if you give ownership before giving permission).

您必须为您的服务帐户电子邮件地址提供所有权。

You have to give ownership to your service account email address.

如果您按错误的顺序执行此操作;您的网站不会列在站点地图列表中,即使您明确尝试通过其地址获取您站点的站点地图,您也会收到身份验证错误。

If you do this in the wrong order; your site will not be listed in sitemaps list and even if you explicitly try to get sitemaps of your site by it's address you will get an authentication error.

您必须为您想要访问的每个域提供权限和所有权(即使只是为了获取他们的名字)。

没有单个顶级所有网站的级别访问控制。

这篇关于Google Webmasters API for Java返回空站点列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆