title: Running Apps Script on Android?
body: <p>I am currently developing an Android app that requires the user to log into  Google, and then get verified by running an Apps Script to figure out whether the user is part of a Google Group (I haven't been able to figure out how to do this outside of using Apps Scripts). I am able to get the user logged in using Firebase Authentication, but no matter what I try I am unable to run the Apps Script (the script itself works since it's able to run from the browser) since I can't seem to get the authorization right (I get 401 and 403 errors).</p>
  
  <p>Here are some things I've tried:</p>
  
  <ol>
  <li><p>Using the Google Apps Script API</p>
  
  <pre><code>Script service = new Script.Builder(new NetHttpTransport(), 
  JacksonFactory.getDefaultInstance(), credentials).setApplicationName(APPLICATION_NAME)
          .build();
  
  ExecutionRequest request = new ExecutionRequest()
          .setFunction("functionName");
  
  Operation op = service.scripts().run(MY_SCRIPT_ID, request).execute();
  
  if (op.getResponse() != null &amp;&amp; op.getResponse().get("result") != null) {
       return op.getResponse().get("result");
  }
  </code></pre></li>
  </ol>
  
  <p>(<a href="https://developers.google.com/apps-script/api/how-tos/execute" rel="nofollow noreferrer">https://developers.google.com/apps-script/api/how-tos/execute</a>)</p>
  
  <ol start="2">
  <li><p>Using a POST Request</p>
  
  <pre><code>URL url = new URL("MY_SCRIPT_URL" + "?access_token=" + ACCESS_TOKEN);
  URLConnection conn = url.openConnection();
  conn.setDoOutput(true);
  
  HttpURLConnection urlConn = (HttpURLConnection) conn;
  urlConn.setRequestProperty("Authorization", "Bearer " + ACCESS_TOKEN);
  
  OutputStreamWriter wr = new OutputStreamWriter(urlConn.getOutputStream());
  wr.write("");
  wr.flush();
  wr.close();
  
  if(urlConn.getResponseCode() == 200){
      BufferedReader reader = new BufferedReader(urlConn.getOutputStream());
      //....AND HERE I READ FROM THE OUTPUT STREAM
  }
  </code></pre></li>
  </ol>
  
  <p>(<a href="https://stackoverflow.com/questions/42194096/run-google-apps-script-in-android">Run Google Apps Script in Android</a>, <a href="https://stackoverflow.com/questions/9140287/how-to-pass-authorization-header-value-in-oauth-2-0-with-google-apis">How to pass &#39;Authorization&#39; header value in OAuth 2.0 with Google APIs</a>, <a href="https://stackoverflow.com/questions/47593376/add-header-to-post-request-in-string">Add header to post request in string</a>, <a href="https://developers.google.com/identity/protocols/OAuth2InstalledApp" rel="nofollow noreferrer">https://developers.google.com/identity/protocols/OAuth2InstalledApp</a>)</p>
  
  <p>Where I've tried making <code>credentials</code> in the following ways:</p>
  
  <ol>
  <li><p>Using a Firebase Authentication Access Token</p>
  
  <pre><code>GoogleCredential credentials = new GoogleCredential()
      .createScoped(Arrays.asList(ScriptScopes.GROUPS))
      .setAccessToken(FIREBASE_ACCESS_TOKEN);
  </code></pre></li>
  <li><p>Using a Service Account</p>
  
  <pre><code>InputStream in = activity.getAssets().open("SERVICE_INFO_FILE");
  
  GoogleCredential credentials = GoogleCredential.fromStream(new FileInputStream(in))
      .createScoped(Arrays.asList(ScriptScopes.GROUPS));
  
  credentials.refreshToken();
  </code></pre></li>
  <li><p>Using a Client Secret</p>
  
  <pre><code>GoogleCredential credentials = new GoogleCredential.Builder()
      .setClientSecrets("CLIENT_ID", "CLIENT_SECRET").build()
      .createScoped(Arrays.asList(ScriptScopes.GROUPS));
  </code></pre></li>
  </ol>
  
  <p>Some other things to note:</p>
  
  <ul>
  <li>The Apps Script is linked to the same Google Cloud project as the service account and Firebase project.</li>
  <li>The client secret was generated for the Apps Scripts API.</li>
  </ul>
  
  <p>So, how do I authenticate properly to access the Apps Script as the user? And is there any other way of accessing what Google Groups the user is a part of?</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>While using the following code, I seem to have gotten it to authenticate properly;</p>
  
  <pre><code>GoogleAccountCredential credentials = GoogleAccountCredential.usingOAuth2(activity, Arrays.asList(ScriptScopes.GROUPS));
  
  credentials.getSelectedAccountName(FirebaseAuth.getInstance().getCurrentUser().getEmail());
  
  Script service = new Script.Builder(new NetHttpTransport(), 
      JacksonFactory.getDefaultInstance(), credentials).setApplicationName(APPLICATION_NAME)
              .build();
  
  ExecutionRequest request = new ExecutionRequest()
              .setFunction("testGroup");
  
  Operation op = service.scripts().run(MY_SCRIPT_ID, request).execute();
  </code></pre>
  
  <p>But when it reaches the last line, the whole app freezes and eventually stops working. I assume this is because the app is waiting for the Apps Script to return something? Any ideas on how to fix this?</p>
  
  <p>Here is the Apps Script code btw:</p>
  
  <pre><code>function testGroup() {
    //return 0;
    var groups = GroupsApp.getGroups();
    for(var i = 0; i &lt; groups.length; i++){
      //Logger.log(groups[i].getEmail());
      if(groups[i].getEmail().equals('group1')){
        return 0;
      }else if(groups[i].getEmail().equals('group2')){
        return 1;
      }
    }
    return -1;
  }
  </code></pre>
  
  <p>(Even when I just return 0 it still doesn't work).</p>