0
votes

When I try to run an Appscript I get error ..

[17-02-08 01:00:35:160 PST] Open the following URL and re-run the script: https://accounts.google.com/o/oauth2/auth?client_id=317559754348->0p1ti3fmjae175i06hn07jrbia6701q6.apps.googleusercontent.com&response_type=code&redirect_uri=https%3A%2F%2Fscript.google.com%2Fmacros%2Fd>%2F1pgAT7ZCwiKrHx_7Iys770hJNRqTYwn9zioe9Qjvmhzc9rfIxO04P8Uum%2Fusercallback&state=ADEpC8xV9BMj3kqytygKLnjEYT7PX918NJg0i1oSCAsUTRwXcOgdzDStZA3lzGDK98CJ6OOhlDnlYEyyji5rx6P8haao8oDop->PMQBZsMMjk2Jl_GtsPnsifFDt1XjqSXtCS2Wx6X3fdLDHTlBzfwqvqrinfkHhW1dVw0oNv6-MaqDhimE912Po&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth>%2Fdevstorage.read_write&access_type=offline&approval_prompt=force&login_hint=imerrywe%40jaguarlandrover.com

Trouble is Client ID 317559754348-0p1ti3fmjae175i06hn07jrbia6701q6.apps.googleusercontent.com

no longer exists. Looks like it has accidentaly been deleted. I have tried created new Oauth2 credentials but my appscript wants to use the old one.

How do I get my appscripts to use new credentials. ?

Regards, Ian

// Global Project variables
var CLIENT_ID = 'xxxxxxxxxx-
leuap166eur7gi5ufr6kiau2nqefknci.apps.googleusercontent.com';
var CLIENT_SECRET = 'xxxxxxxxxx';

// OAuth2.0 Access token
var token;

function oAuth() {
    // Check we have access to the service
  Logger.log(Session.getActiveUser().getEmail());
  Logger.log( Session.getEffectiveUser().getEmail());

    var service = getService();
    Logger.log('Access '+service.hasAccess());
  var authInfo = ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL);
 Logger.log(authInfo.getAuthorizationStatus());
    if (!service.hasAccess()) {
        var authorizationUrl = service.getAuthorizationUrl();
        Logger.log('Open the following URL and re-run the script: %s', 
authorizationUrl);
        return;
    }
}

  function getService() {
// Create a new service with the given name. The name will be used when
// persisting the authorized token, so ensure it is unique within the
// scope of the property store.
return OAuth2.createService('xxxxxxxxxx')

    // Set the endpoint URLs, which are the same for all Google services.
    .setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
    .setTokenUrl('https://accounts.google.com/o/oauth2/token')


    // Set the client ID and secret, from the Google Developers Console.
    .setClientId(CLIENT_ID)
    .setClientSecret(CLIENT_SECRET)

    // Set the name of the callback function in the script referenced
    // above 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 scopes to request (space-separated for Google services).
    // this is admin access for the sqlservice and access to the cloud-        platform:
.setScope('https://www.googleapis.com/auth/sqlservice.admin 
https://www.googleapis.com/auth/cloud-platform')

    // Below are Google-specific OAuth2 parameters.

    // Sets the login hint, which will prevent the account chooser screen
    // from being shown to users logged in with multiple accounts.
    .setParam('login_hint', Session.getActiveUser().getEmail())

    // Requests offline access.
    .setParam('access_type', 'offline')

    // Forces the approval prompt every time. This is useful for testing,
    // but not desirable in a production application.
    .setParam('approval_prompt', 'force');
}
  1. That’s an error.

Error: redirect_uri_mismatch

The redirect URI in the request, does not match the ones authorized for the OAuth >client. Visit >1 to update the authorized redirect URIs.

1
Do you have access to the apps script project? Do you know how to edit in the IDE? - Spencer Easton
I am writing the scripts at script.google.com. The scripts have previously worked ok. - mez63
I have added a simple script that re-creates the error. I tried setting variables to a different client_id but it seems to ignore this. - mez63
show the code where you setup getService(). Also is the client ID really broken over two lines in your code? It won't work as you have it in your example. - Spencer Easton
Thanks for your help. Slowly making progress, I didn't realise the getservice() function existed in a different script someone had sent me and was in my project. It was picking the code up from there. I have added the code. It is now trying to use the client_id specified but is still not authorised. When I copy and paste the authorisation url I now get an another error. I have added that to original post. - mez63

1 Answers

0
votes

Just to address the

  1. That’s an error. Error: redirect_uri_mismatch

Go to your new clientID, below it you may need to add the "oauth2callback" redirect uri like:

http://www.mywebsite.com/oauth2callback

enter image description here