OAuth2 + Google API Webmasters: Download Latest SEO Links, Unauthorized 401 Error

INTRO

To automate some reporting, I registered the project in the Google Developer Console, enabled the Webmaster Tools API, Site Validation API.

(For areas:

 String [] SCOPESArray= {"https://www.googleapis.com/auth/webmasters", 
                      "https://sites.google.com/feeds/"};

      

)

Then I created the service account credentials in my Java application:

 GoogleCredential credential = new  GoogleCredential.Builder().setTransport(httpTransport)
                    .setJsonFactory(jsonFactory)
                    .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
                    .setServiceAccountScopes(SCOPES)
                    .setServiceAccountPrivateKeyFromP12File(p12)
                    .build();

      

and service:

    Webmasters service = new Webmasters.Builder(httpTransport, jsonFactory, credential).setApplicationName("APP NAME").build();

      

Then I got all verified sites successfully:

 Webmasters.Sites.List request = service.sites().list();
   SitesListResponse siteList = request.execute();
   for (WmxSite currentSite : siteList.getSiteEntry()) {
    System.out.println(currentSite+": "+ currentSite.getPermissionLevel());
    }

      

Output:

{"permissionLevel":"siteFullUser","siteUrl":"http://www.mysite1.com/"}: siteFullUser
{"permissionLevel":"siteOwner","siteUrl":"http://www.mysite2.net/"}: siteOwner

      

PROBLEM:

How to download the latest links *. CSV file from Search Traffic \ Links to my site? (The page https://www.google.com/webmasters/tools/external-links-domain?hl=en&siteUrl=http://www.mysite2.net/ contains a link to this file).

My idea was to use the query as the first step:

String access_token = credential.getAccessToken();
HttpRequestFactory requestFactory = service.getRequestFactory(); 
GenericUrl url = new GenericUrl("https://www.google.com/webmasters/tools/external-links-domain?hl=en&siteUrl=http://www.mysite2.net/?access_token="+access_token);
HttpRequest httpRequest = requestFactory.buildGetRequest(url);
HttpResponse response = httpRequest.execute();

      

But I am getting the error:

HTTP/1.1 401 Unauthorized
Alternate-protocol: 443:quic,p=1
Content-length: 147
X-xss-protection: 1; mode=block
X-content-type-options: nosniff
Transfer-encoding: chunked
Expires: Sat, 13 Jun 2015 17:00:20 GMT
Server: GSE
-content-encoding: gzip
Cache-control: private, max-age=0
Date: Sat, 13 Jun 2015 17:00:20 GMT
X-frame-options: SAMEORIGIN
Content-type: text/html; charset=UTF-8
Www-authenticate: Bearer realm="https://www.google.com/accounts/"

      

Note: The previous OAuth + Webmaster (V2) combination worked great for me: the response to a similar request contained a link to the required * .csv file

+3


source to share





All Articles