apps-script-oauth2: Auth Token flow does not complete - redirects to Broken Gdrive page

I am attempting to use this library for a GMail add-on. My code is essentially a copy/past of the Non-Google OAuth configuration example from here: https://developers.google.com/gmail/add-ons/how-tos/non-google-services - which pretty much calls methods from this library to build a card and initiate auth-flow.

From everything I understand, the auth-flow itself is fully handled by the library - at the end of which, I can access my auth token.

However, in my case, the auth-flow stops after triggering the redirect URL:

https://script.google.com/a/macros/workboard.com/{script_id}/usercallback/?success=1&code={code}&state={state}. 

At this point, the auth pop-window has the above URL (with actual values, not placeholders) and a drive message saying:

“Sorry, unable to open the file at this time.”

I have tried debugging this on my end - but since the entire auth-flow should happen inside the library, can’t get much further. Would appreciate any pointers to on what the issue might be or what I can do to debug this further.

Thanks, Bhavin.

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 8
  • Comments: 37 (9 by maintainers)

Most upvoted comments

Apps script (DataStudio Community connector) failing with a Google Drive error.

We had the same issue, our use case is a custom Data Studio Community connector using a standalone AppScript that is meant to fetch data from Cloud Spanner. We are using OAuth2 provider(Tyk) and the OAuth2 Appscript library as specified.

Redirect URI for AppScript is setup as https://script.google.com/macros/d/{SCRIPT ID}/usercallback as per the this library documentation. During script execution the callback step is failing with a Google Drive error message.

Could you please guide us as to the best way to resolve this issue ? Also, any documentation or insight into which repository/library we should use for AppScript will be appreciated. issue

I am also encountering this issue with the callback url as specified in the documentation and generated by service.getRedirectUri()

As a temporary workaround, I have:

  •      defined a `doGet()` function
    
  •      check for the "code" parameter in the get request
    
  •      manually pass the request to oauthCallback if "code" is present. 
    
  •      published my script as a web-app
    
  •      set redirect url on my OAuth Provider to the web-app url
    
  •      override Oauth2 "generateRedirectUri" to return published script URI
    
function doGet(e){
    if(e.parameter['code']){
        console.log('authorization code in request, do callback');
        return authCallback(e);
    }

    var hoverService = getHoverService();  //get service for hover.to
    if(!hoverService.hasAccess()){
        //display authorization link
    }else{
        //do test request
    }
}
OAuth2.getRedirectUri = function(scriptId) {
  return ScriptApp.getService().getUrl();
}

I have recently looped back to this issue as I’m using the library on another project and bumped into the same issue. On further testing in this case I have isolated the issue to a situation where the “state” query string parameter is missing from the request to /usercallback.

https://script.google.com/a/macros/interlockit.com/d/xxxxx/usercallback?state=a receives a valid response: “The state token is invalid or has expired. Please try again.”

https://script.google.com/a/macros/interlockit.com/d/xxxxx/usercallback?code=xxx&expires_in=1111 receives the error: “Page Not Found” / “Sorry, unable to open the file at this time. Please check the address and try again…”

It appears the original reporter was passing in the state, so I’m not sure how relevant this is overall, I can’t recall the specifics of my last case where I implemented the doGet() workaround.

I had this same issue and and eventually figured out that I was calling .setParam('response_type', 'token') instead of .setParam('response_type', 'code') which was required by the specific api I was using. Just a warning to those who might spend a lot of time trying to debug the source code 😛 when that’s not the real issue 😛

I am also encountering this issue with the callback url as specified in the documentation and generated by service.getRedirectUri()

As a temporary workaround, I have:

  •      defined a `doGet()` function
    
  •      check for the "code" parameter in the get request
    
  •      manually pass the request to oauthCallback if "code" is present. 
    
  •      published my script as a web-app
    
  •      set redirect url on my OAuth Provider to the web-app url
    
  •      override Oauth2 "generateRedirectUri" to return published script URI
    
function doGet(e){
    if(e.parameter['code']){
        console.log('authorization code in request, do callback');
        return authCallback(e);
    }

    var hoverService = getHoverService();  //get service for hover.to
    if(!hoverService.hasAccess()){
        //display authorization link
    }else{
        //do test request
    }
}
OAuth2.getRedirectUri = function(scriptId) {
  return ScriptApp.getService().getUrl();
}

This approach really worked well for me.