我目前正在开发一个Android应用,该应用要求用户登录Google,然后通过运行Apps脚本进行验证,以查明该用户是否属于Google网上论坛(我无法查明如何在不使用Apps脚本的情况下执行此操作)。我可以使用户使用Firebase身份验证登录,但是无论如何尝试,我都无法运行Apps脚本(脚本本身可以运行,因为它可以从浏览器运行),因为我似乎无法获得授权权限(我收到401和403错误)。
这是我尝试过的一些事情:
使用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)
使用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)
我曾尝试通过以下方式制作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脚本?还有其他方法可以访问用户所属的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脚本代码:
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仍然不起作用)。
答案 0 :(得分:1)
将Apps Script API Client Library for Java与oauth2一起使用:
implementation "com.google.apis:google-api-services-script:v1-rev271-1.25.0"