C
C
cajka-d2021-11-08 16:52:55
OAuth
cajka-d, 2021-11-08 16:52:55

Why is the redirect_uri wrong when using the OAuth2 library for Goole Apps Script?

Hello.

I am writing an application in Google Apps Script language. In this application, I want to use the VKontakte API. To interact with the VKontakte API from an application in the Google Apps Script language, I added the OAuth2 library to the development environment. Link to the OAuth2 library . I did everything as described in the documentation on the link. The code itself works, but the link that is generated for the redirect_uri parameter is not working. That is, if you click on it, Google writes that the file will not be found. Vkontakte also returns an error when authorizing via OAuth2 that redirect_uri was not found.

Script code:

var CLIENT_ID = 'client id';
var CLIENT_SECRET = 'client secret';

/**
 * Authorizes and makes a request to the VK API.
 */
function run() {
  var service = getService();
  if (service.hasAccess()) {
    // GET requests require access_token parameter
    var url = 'https://api.vk.com/method/users.get?fields=first_name,last_name&access_token=' + service.getAccessToken();
    var response = UrlFetchApp.fetch(url);

    // note: add 'email' to the fields= list above in order to
    //       fetch email from token (+uncomment 2 lines below)
    // var email = service.getToken().email;
    // Logger.log('email: '+email);

    var result = JSON.parse(response.getContentText());
    Logger.log(JSON.stringify(result, null, 2));
  } else {
    var authorizationUrl = service.getAuthorizationUrl();
    Logger.log('Open the following URL and re-run the script: %s',
        authorizationUrl);
  }
}

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

/**
 * Configures the service.
 */
function getService() {
  return OAuth2.createService('VK')
      // Set the endpoint URLs.
      .setAuthorizationBaseUrl('https://oauth.vk.com/authorize')
      .setTokenUrl('https://oauth.vk.com/access_token')

      // Set the client ID and secret.
      .setClientId(CLIENT_ID)
      .setClientSecret(CLIENT_SECRET)

      // 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 specific parameters if its are supported
      .setScope('email,groups,offline');
}

/**
 * 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.');
  }
}

/**
 * Logs the redict URI to register in the VK Aps Page https://vk.com/apps?act=manage.
 */
function logRedirectUri() {
  Logger.log(OAuth2.getRedirectUri());
}


To get the URL for the redirect_uri, you can simply run the logRedirectUri function .

Can anyone suggest what I'm doing wrong? And what needs to be done so that the returned URL for redirect_uri is working when accessed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ivanov, 2021-11-09
@cajka-d

The redirect does not need to be specified in the application settings. He's not for that.
6189d3fa84698507725762.png
Still, I think you need to pass the API version

const url =
  'https://api.vk.com/method/users.get?&v=5.131&fields=first_name,last_name&access_token=' +
    service.getAccessToken();

https://github.com/googleworkspace/apps-script-oau...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question