Home > Blockchain >  Get the user of the creator of a web app script
Get the user of the creator of a web app script

Time:11-18

I want to use a Google Web App to access the gmail of the web app creator. I need to also know the name of the user who created the web app. I discovered that when you create the web app to be used "by anyone" then it's not possilbe to get the creator's email using Session:

function returnUser() {

  var user = Session.getActiveUser().getEmail();
  return ContentService.createTextOutput("User is " user)
}

function doGet(){
  
  return returnUser();

}

When I call this web app from a browser without being logged in the user returned is blank.

Just to emphasize I an NOT interested in the user who is calling the App. I want the user email of the script creator.

Edit: I found I can go into the inbox and count the mail recipients from the first few threads. The most common emial is likely to be the inbox owner. Not perfect but good enough. If you have a better solution please feel free.

 //get user's email. When using web app Session.getActiveUser().getEmail() doesn't work 
function getTo(){ 
  console.log(Session.getActiveUser().getEmail())
  var threads = GmailApp.search("to:me",0,10);
  var counter = 0;
  var recipients ={};

  //get all message recipeints and count them into a JSON object.
  threads.forEach((thread) =>{
    var messages = thread.getMessages()
    messages.forEach((message) =>{
      var recipient = message.getTo();
      if(recipients[recipient] == undefined) recipients[recipient] = 1;
      else recipients[recipient] = recipients[recipient] 1;
    })
  })

  //get most common key
  var keys = Object.keys(recipients);
  var max = 0;
  var mostCommonEmail = '';
  keys.forEach(key => {
    if(recipients[key]>max) {
      max=recipients[key];
      mostCommonEmail = key;

    }
  })
  return mostCommonEmail;
}

CodePudding user response:

In your situation, how about the following modification?

From:

var user = Session.getActiveUser().getEmail();

To:

var user = Session.getEffectiveUser().getEmail();

Note:

Reference:

CodePudding user response:

As @Tanike mentioned, you are probably looking for getEffectiveUser(), as the documentation says:

If the script is a web app set to "execute as me" (the developer), this returns the developer's user account.

Using Apps Script REST API

If you want further information about the Web App Deployments or Projects you always can use the Apps Script REST API.

For example, if you want to know who is the creator of certain script you can use:

function restApiAS(){
  let token = ScriptApp.getOAuthToken()
  let projectId = ScriptApp.getScriptId()
  let options = {headers: {"Authorization": "Bearer "   token}}
  let url_project = "https://script.googleapis.com/v1/projects/"   projectId
  let dataScript = UrlFetchApp.fetch(url_project, options)
  Logger.log(dataScript)
  return JSON.parse(dataScript).creator.email
}

To use this functionality, you must change your association to a user-managed GCP project, enable the Apps Script API, and add these scopes to your appsscript.json:

  "oauthScopes": [
    "https://www.googleapis.com/auth/script.projects",
    "https://www.googleapis.com/auth/script.external_request",
  ]
  • Related