Andrew Charlton
Andrew Charlton

Reputation: 251

Google Indexing API: Permission denied. Failed to verify the URL ownership

I've looked through all of the other threads on this and unfortunately they still haven't resolved my issue, so I'm hoping you can help! I'm trying to use Google's Indexing API via Google App Script. So far I've:

Followed the documentation step-by-step; created a service account, connected my app script to my GC project and added my client email as an owner of a search console property.

I've also included my app script manifest file to include the following oauthScopes:

    "oauthScopes": [
    "https://www.googleapis.com/auth/indexing",
    "https://www.googleapis.com/auth/script.external_request",
    "https://www.googleapis.com/auth/userinfo.email"
  ]

Most of the 403 errors I've read about are resolved by adding the client email as an owner of a Search Console property, but in my case, that hasn't resolved the issue. Happy to provide more details if needed! :)

Here's my Google App Script:

var Json = {
    "private_key": "###",
    "client_email": "###",
    "client_id": "###",
    "user_email": "###"
};


/**
 * Authorizes and makes a request to the Google+ API.
 */
function run() {

  var requestBody = {
       "url":"http://testymctestface.com",
       "type":"URL_UPDATED"
}

   var options = {
        'method': 'POST',
        'contentType': 'application/json',
        'headers': {
            'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
        },
        'payload': JSON.stringify(requestBody),
        'muteHttpExceptions': true
    };

        var service = getService();
        var url = 'https://indexing.googleapis.com/v3/urlNotifications:publish';
        var response = UrlFetchApp.fetch(url,options);
        Logger.log(response);

}

/**
 * Reset the authorization state, so that it can be re-tested.
 */
function reset() {

  var service = getService();
  service.reset();
}

/**
 * Configures the service.
 */
function getService() {
  return OAuth2.createService('Indexing API')
      // Set the endpoint URLs.
      .setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
      .setTokenUrl('https://accounts.google.com/o/oauth2/token')

      // Set the client ID and secret.
      .setPrivateKey(Json.private_key)
      .setIssuer(Json.client_email)
      .setSubject(Json.user_email)
      .setClientId(Json.client_id)

      // Set the name of the callback function that should be invoked to complete
      // the OAuth flow.
      .setCallbackFunction('authCallback')

      // Set the property store where authorized tokens should be persisted.
      .setPropertyStore(PropertiesService.getUserProperties())

      // Set the scope and additional Google-specific parameters.
      .setScope('https://www.googleapis.com/auth/indexing')
      .setParam('access_type', 'offline')
      .setParam('approval_prompt', 'force')
      .setParam('login_hint', Session.getActiveUser().getEmail());
}

/**
 * Handles the OAuth callback.
 */
function authCallback(request) {
  var service = getService();
  var authorized = service.handleCallback(request);
  if (authorized) {
    return HtmlService.createHtmlOutput('Success!');
  } else {
    return HtmlService.createHtmlOutput('Denied');
  }
}

Upvotes: 1

Views: 3055

Answers (2)

Gekicalle
Gekicalle

Reputation: 1

you have to change this line

'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()

in

'Authorization': 'Bearer '+ service.getAccessToken()

Upvotes: -1

kevinharvey
kevinharvey

Reputation: 858

Did you add your client email in the new Search Console, or the old Search Console? "Owner" was not an option for me in the new one (only "Full" or "Restricted"), I had to go to the old one to add another owner.

Upvotes: 8

Related Questions