首页 文章

Google API多个范围

提问于
浏览
6

我想在Google API JavaScript中使用多个范围 . 我想要做的是获取用户的电子邮件和姓名 .

我可以从范围中获得的名称:https://www.googleapis.com/auth/plus.me

我可以从范围获得电子邮件:https://www.googleapis.com/auth/userinfo.email

但是如何在同一个应用程序中使用它们呢?

我的代码是:

scopes = 'https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.email';

    gapi.signin.render('StartGoogleBtn', {
      'callback': 'onSignInCallback',
      'clientid': clientId,
      'cookiepolicy': 'single_host_origin',
      'scope': scopes
    });

应该是什么范围?谢谢 :)

3 回答

  • 8

    只是猜测,但尝试用围绕它的双引号分隔的空格 . 这就是OAuth 2所要求的,当我为客户端编写代码时,我会自动正确地处理该场景 . 我认为它只是通过命令传递 . 这对我有用 .

    scopes = "https://www.googleapis.com/auth/userinfo.email  https://www.googleapis.com/auth/plus.me"
    
  • 1

    我将通过说我没有使用Javascript版本来解释我的回答 . 我正在使用Java库,它允许我通过传递包含我想要的范围的字符串列表来设置范围 .

    List<String> SCOPES = Arrays.asList(
            DirectoryScopes.ADMIN_DIRECTORY_GROUP, //https://www.googleapis.com/auth/admin.directory.group
            DirectoryScopes.ADMIN_DIRECTORY_USER,  //https://www.googleapis.com/auth/admin.directory.user
            DriveScopes.DRIVE                      //https://www.googleapis.com/auth/drive
            );
    
    credential = new GoogleCredential.Builder()
                    .setTransport(httpTransport).setJsonFactory(jsonFactory)
                    .setServiceAccountId(serviceAccountId)
                    .setServiceAccountScopes(SCOPES)
                    .setServiceAccountPrivateKeyFromP12File(p12File)
                    .setServiceAccountUser(adminUser).build();
    

    假设Javascript库的工作方式与Java类似,那么您应该能够通过使 scopes 变量成为字符串数组来添加多个范围 .

  • 0

    这就是您如何正确地逐步缩小范围这是您在appscript.json中的Google应用程序脚本上编写范围时应遵循的格式

    {"oauthScopes":[“https://www.googleapis.com/auth/spreadsheets " , " https://www.googleapis.com/auth/script.send_mail”]}

相关问题