1

I've searched far and wide and have come up with little to nothing on this problem. I've made a module for my Yii app that crawls my website and gathers links to generate a sitemap, I've even made it so that it can run on a cron.

Now I've hit the wall with Google Webmaster Tools API and it's lack of information on how to implement it with OAuth2 for sitemap submission.

Every time I've tried to submit the sitemap I got this response back:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
 }
}

I would very much appreciate any pointers in any direction.

Aleksandar B.
  • 128
  • 1
  • 9

2 Answers2

0

Maybe this extension can help you:

http://www.yiiframework.com/extension/jgoogleapi/

I'm not sure which is the API for sitemaps and what are its methods but that extension would help you Login to google in "Service" mode that won't need your browser to interact with the login.

You should also before create your application in Google console and then create a service account user type for it.

A paste of my config file when using this extension with Google Analytics:

<?php
/*
 * How to obtain a Service Account:
 * https://developers.google.com/accounts/docs/OAuth2ServiceAccount
 * 
 * 
 * (403) User does not have any Google Analytics account.
 * http://stackoverflow.com/a/13167988/115050
 * 
 * 
 */
return array(
    'class' => 'ext.JGoogleAPI.JGoogleAPI',
    //Default authentication type to be used by the extension
    'defaultAuthenticationType'=>'serviceAPI',

    //Account type Authentication data
    'serviceAPI' => array(
        'clientId' => '...',
        'clientEmail' => '...',
        'keyFilePath' => dirname(__FILE__).'/../extensions/JGoogleAPI/keys/Analytics-a0e8e345f273.p12',
    ),
    /*
    //You can define one of the authentication types or both (for a Service Account or Web Application Account) 
    webAppAPI = array(
        'clientId' => 'YOUR_WEB_APPLICATION_CLIENT_ID',
        'clientEmail' => 'YOUR_WEB_APPLICATION_CLIENT_EMAIL',
        'clientSecret' => 'YOUR_WEB_APPLICATION_CLIENT_SECRET',
        'redirectUri' => 'YOUR_WEB_APPLICATION_REDIRECT_URI',
        'javascriptOrigins' => 'YOUR_WEB_APPLICATION_JAVASCRIPT_ORIGINS',
    ),
    */
    'simpleApiKey' => 'AIzaSyAx63Ht-0XmuLdp0-j9zVREKNsCyqXgeUA',

    //Scopes needed to access the API data defined by authentication type
    'scopes' => array(
        'serviceAPI' => array(
            'drive' => array(
                'https://www.googleapis.com/auth/drive.file',
            ),
            'Analytics'=>array(
                'https://www.googleapis.com/auth/analytics.readonly',
            ),
        ),
        'webappAPI' => array(
            'drive' => array(
                'https://www.googleapis.com/auth/drive.file',
            ),
        ),
    ),
    //Use objects when retriving data from api if true or an array if false
    'useObjects'=>false,
);

And how I'm using it:

$api = Yii::app()->JGoogleAPI->getService('Analytics');
$api->data_ga->get(...)
  • Thanks for the answer but I have already tried this extension. It doesn't support the API I need. It literally has every Google API except the Webmaster Tools API. For now I've settled for just pinging the search engine with my sitemap but I'm still on the hunt for an existing solution. If I don't find anything I'll probably make my own module or extension when I steal away some time to actually do the research and experimenting needed. – Aleksandar B. Feb 04 '15 at 19:52
0

Your access code is invalid. Use refresh token to avoid getting error while authenticating google client.

use the below code:

$gClient->setAccessType("offline");// to get refresh token after expiration of access token
$gClient->setIncludeGrantedScopes(true); 
Subhajit
  • 876
  • 3
  • 17
  • 37