我目前正在开发一个 Android 应用程序,该应用程序要求用户登录 Google,然后通过运行 Apps 脚本来确定用户是否属于 Google Group 进行验证(我无法弄清楚该怎么做这在使用 Apps 脚本之外)。我可以让用户使用 Firebase 身份验证登录,但无论我尝试什么,我都无法运行 Apps 脚本(脚本本身可以工作,因为它可以从浏览器运行),因为我似乎无法获得授权权限(我得到 401 和 403 错误)。
以下是我尝试过的一些事情:
使用 Google Apps 脚本 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)
使用 POST 请求
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 }
(在 Android 中运行 Google Apps 脚本,如何使用 Google API 在 OAuth 2.0 中传递“授权”标头值,添加标头以在字符串中发布请求,https://developers.google.com/identity/protocols/OAuth2InstalledApp)
我尝试credentials
通过以下方式制作的地方:
使用 Firebase 身份验证访问令牌
GoogleCredential credentials = new GoogleCredential() .createScoped(Arrays.asList(ScriptScopes.GROUPS)) .setAccessToken(FIREBASE_ACCESS_TOKEN);
使用服务帐户
InputStream in = activity.getAssets().open("SERVICE_INFO_FILE"); GoogleCredential credentials = GoogleCredential.fromStream(new FileInputStream(in)) .createScoped(Arrays.asList(ScriptScopes.GROUPS)); credentials.refreshToken();
使用客户端密码
GoogleCredential credentials = new GoogleCredential.Builder() .setClientSecrets("CLIENT_ID", "CLIENT_SECRET").build() .createScoped(Arrays.asList(ScriptScopes.GROUPS));
其他一些需要注意的事项:
- Apps 脚本与服务帐户和 Firebase 项目链接到同一个 Google Cloud 项目。
- 客户端密码是为 Apps Scripts API 生成的。
那么,我如何正确地进行身份验证才能以用户身份访问 Apps 脚本?还有其他方法可以访问用户所属的 Google 群组吗?
提前致谢!
编辑
在使用以下代码时,我似乎已经得到了正确的身份验证;
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();
但是当它到达最后一行时,整个应用程序冻结并最终停止工作。我认为这是因为应用程序正在等待应用程序脚本返回一些东西?有想法该怎么解决这个吗?
这是应用程序脚本代码顺便说一句:
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;
}
(即使我只返回 0 它仍然不起作用)。