0

I am pretty novice when it comes to App Scripts and the entire OAuth process in general. I am working on a script that would be able to either read the Subject line information or the email itself(whichever is do-able),of a secondary email address with the Gmail API. From what I've read you need to set up OAuth configurations(which I think I have done correctly), After a bunch of messing around I was able to finally get a URL to appear to allow access, then more errors appeared. I finally got an error the says something about my script missing a function called "doGet". From reading similar cases I don't recall anyone mentioning a doGet function. I'm basically asking for advice or guidance to make sure I'm doing it right and not going the wrong way.

Once I changed a term in my script, I was able to get the URl to appear, I then went to that URl under the secondary account that I'm trying to use for the script. After getting URI redirect mismatch errors, I finally fixed that. I know get a "Script Function not found: doGet" error page, after I select "Allow" on the secondary email account. From doing readings on similar situations, I don't recall a doGet function being used by other examples, So I don't know if I'm going the right direction or just going in circles. I am now getting a a notice after I select allow, "the script has completed but did not return anything" It should be returning the subject info from the newest email.(edited 7/19/23)

function authorize() {
  var oauth2 = OAuth2.createService('Gmail')
    .setClientId(CLIENT_ID)
    .setClientSecret(CLIENT_SECRET)
    .setRedirectUri(REDIRECT_URI)
    .setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
    .setTokenUrl('https://accounts.google.com/o/oauth2/token')
    .setScope(SCOPES.join(' '))
    .setParam('access_type', 'offline')
    .setParam('approval_prompt', 'force')
    .setCallbackFunction('authCallback');

  var authorizationUrl = oauth2.getAuthorizationUrl();
}

function authCallback(request) {
  var oauth2 = OAuth2.createService('Gmail')
    .setClientId(CLIENT_ID)
    .setClientSecret(CLIENT_SECRET)
    .setRedirectUri(REDIRECT_URI)
    .setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
    .setTokenUrl('https://accounts.google.com/o/oauth2/token')
    .setScope(SCOPES.join(' '))
    .setParam('access_type', 'offline')
    .setParam('approval_prompt', 'force');
  var authorized = oauth2.handleCallback(request);
  if (authorized) {
    var accessToken = oauth2.getAccessToken();
    var threads = Gmail.Users.Threads.list('secondaryemailaccount.com', { accessToken: accessToken });
  } else {
    Logger.log('Authorization denied.');
  }
}
  • You have methold to retrive all thread and the in each thread you have getMessages() so you can go through all of the messages and pick the one's you wish – Cooper Jul 18 '23 at 14:27
  • i will give that a try – Matthew Kapfer Jul 18 '23 at 15:19
  • Not sure if you've figured this out or not, but `doGet` is usually referring to a web app. When you hit the deployed url of a Google Web App, Google expects there to be a `doGet` function. Is your redirect URI pointing to a deployed web app? – Miguel Rivera Rios Jul 19 '23 at 00:07
  • yes, i deployed the script as a web app and the redirect uri is pointing back to the deployed script web app, I also added a getMessage() as stated above and now after i select allow, a message comes back saying "The script completed but did not return anything" @MiguelRiveraRios – Matthew Kapfer Jul 19 '23 at 12:24
  • Gotcha, I definitely think your OAuth config is incorrect, for example, you should only need to create the OAuth service in your `authorize` method and not in the `authCallback`. You can maybe find a more complete example in this [answer](https://stackoverflow.com/a/74031018/13771937). You can also find more on the official docs for the [OAuth library](https://github.com/googleworkspace/apps-script-oauth2). If that still doesn't get you what you need, can you explain exactly what you want to happen and I can maybe help get you going. – Miguel Rivera Rios Jul 19 '23 at 14:19
  • Yes, let me see if I can get it based off that link you shared, and i will report back. – Matthew Kapfer Jul 19 '23 at 16:20
  • Also, what I'm trying to do is, retrieve the SUBJECT line info from a secondary email address via app script. that is all. – Matthew Kapfer Jul 19 '23 at 17:08

1 Answers1

0

So I ended up just doing it the easy way, it works for my use case. I just made a new owner of the sheet/scripts by copying the information and making the secondary email address I need, the owner. I am able to get the information I need.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 26 '23 at 11:40