我目前正在开发一款
Android应用,需要用户登录Google,然后通过运行应用脚本来验证用户是否属于Google群组(我无法弄清楚如何操作除了使用Apps Scripts之外.我能够让用户使用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
}
(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)
我尝试通过以下方式创建凭据:
>使用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();
但当它到达最后一行时,整个应用程序冻结并最终停止工作.我认为这是因为应用程序正在等待Apps脚本返回一些东西?有想法该怎么解决这个吗?
这是Apps脚本代码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;
}
(即使我只返回0,它仍然不起作用).
最佳答案 将
Apps Script API Client Library for Java与
oauth2一起使用:
implementation "com.google.apis:google-api-services-script:v1-rev271-1.25.0"