在Android上运行Apps脚本?

时间:2018-10-17 21:38:23

标签: android google-apps-script oauth-2.0 firebase-authentication google-apps-script-api

我目前正在开发一个Android应用,该应用要求用户登录Google,然后通过运行Apps脚本进行验证,以查明该用户是否属于Google网上论坛(我无法查明如何在不使用Apps脚本的情况下执行此操作)。我可以使用户使用Firebase身份验证登录,但是无论如何尝试,我都无法运行Apps脚本(脚本本身可以运行,因为它可以从浏览器运行),因为我似乎无法获得授权权限(我收到401和403错误)。

这是我尝试过的一些事情:

  1. 使用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

  1. 使用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 AndroidHow to pass 'Authorization' header value in OAuth 2.0 with Google APIsAdd header to post request in stringhttps://developers.google.com/identity/protocols/OAuth2InstalledApp

我曾尝试通过以下方式制作credentials

  1. 使用Firebase身份验证访问令牌

    GoogleCredential credentials = new GoogleCredential()
        .createScoped(Arrays.asList(ScriptScopes.GROUPS))
        .setAccessToken(FIREBASE_ACCESS_TOKEN);
    
  2. 使用服务帐户

    InputStream in = activity.getAssets().open("SERVICE_INFO_FILE");
    
    GoogleCredential credentials = GoogleCredential.fromStream(new FileInputStream(in))
        .createScoped(Arrays.asList(ScriptScopes.GROUPS));
    
    credentials.refreshToken();
    
  3. 使用客户机密

    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脚本代码:

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仍然不起作用)。

1 个答案:

答案 0 :(得分:1)

Apps Script API Client Library for Javaoauth2一起使用:

implementation "com.google.apis:google-api-services-script:v1-rev271-1.25.0"