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).
Here are some things I've tried:
-
Using the Google Apps Script API
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 && op.getResponse().get("result") != null) { return op.getResponse().get("result"); }
(https://developers.google.com/apps-script/api/how-tos/execute)
-
Using a POST Request
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 }
(Run Google Apps Script in Android, How to pass 'Authorization' header value in OAuth 2.0 with Google APIs, Add header to post request in string, https://developers.google.com/identity/protocols/OAuth2InstalledApp)
Where I've tried making credentials
in the following ways:
-
Using a Firebase Authentication Access Token
GoogleCredential credentials = new GoogleCredential() .createScoped(Arrays.asList(ScriptScopes.GROUPS)) .setAccessToken(FIREBASE_ACCESS_TOKEN);
-
Using a Service Account
InputStream in = activity.getAssets().open("SERVICE_INFO_FILE"); GoogleCredential credentials = GoogleCredential.fromStream(new FileInputStream(in)) .createScoped(Arrays.asList(ScriptScopes.GROUPS)); credentials.refreshToken();
-
Using a Client Secret
GoogleCredential credentials = new GoogleCredential.Builder() .setClientSecrets("CLIENT_ID", "CLIENT_SECRET").build() .createScoped(Arrays.asList(ScriptScopes.GROUPS));
Some other things to note:
- The Apps Script is linked to the same Google Cloud project as the service account and Firebase project.
- The client secret was generated for the Apps Scripts API.
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?
Thanks in advance!
EDIT
While using the following code, I seem to have gotten it to authenticate properly;
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();
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?
Here is the Apps Script code btw:
function testGroup() {
//return 0;
var groups = GroupsApp.getGroups();
for(var i = 0; i < 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;
}
(Even when I just return 0 it still doesn't work).
from Running Apps Script on Android?
No comments:
Post a Comment