Search code examples
google-apps-scriptgmailgoogle-oauthgmail-api

How can I access a secondary email to retrieve subject line info?


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

Solution

  • 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.