0
votes

I am using this OAuth library in Google Apps script https://github.com/googlesamples/apps-script-oauth2/blob/master/samples/Smartsheet.gs

and trying to make a call to Google Site Verification API, however I get following error while doing it-:

Request failed for https://www.googleapis.com/siteVerification/v1/token returned code 500. Truncated server response: {"error":{"errors":[{"domain":"global","reason":"backendError","message":"Backend Error"}],"code":500,"message":"Backend Error"}} (use muteHttpExceptions option to examine full response) (line 14, file "Service")

function run() {
  var service = getiService();
  if (service.hasAccess()) {

    var url = 'https://www.googleapis.com/siteVerification/v1/token';

    var result = UrlFetchApp.fetch(url, {
      'headers': {
        'Authorization' : 'Bearer ' + service.getAccessToken()
      },
      'method': 'GET',

  "site": {
    "identifier": "somedomainname.com",
    "type": "INET_DOMAIN"
  },
  "verificationMethod": "DNS_TXT"

                                   });

 Logger.log(result)   

Your help is appreciated. thank you

1

1 Answers

1
votes

In my environment, the OAuth library that you say couldn't be used. So I retrieved access token and refresh token by myself and confirmed that this script works. If the library also cannot be used at your environment, please retrieve access token and refresh token.

If you want to get a verification token using the endpoint of https://www.googleapis.com/siteVerification/v1/token, you can see the reference WebResource: getToken. According to the reference, the method is "post". And at UrlFetchApp.fetch(), it writes the request body to payload. When these are reflected to the script, the script becomes as follows.

Modified script :

var url = 'https://www.googleapis.com/siteVerification/v1/token';
var requestBody = {
  site: {
    identifier: "somedomainname.com",
    type: "INET_DOMAIN",
  },
  verificationMethod: "DNS_TXT",
};
var result = UrlFetchApp.fetch(url, {
  headers: {'Authorization': 'Bearer ' + service.getAccessToken()}, // In my environment, service.getAccessToken() was changed to access token I retrieved.
  method: 'post',
  payload: JSON.stringify(requestBody),
  contentType: 'application/json',
  muteHttpExceptions: true,
});

Result :

{
  "method": "DNS_TXT",
  "token": "google-site-verification=#####"
}