Google app engine 从Google Apps脚本访问经过身份验证的Google云端点API

Google app engine 从Google Apps脚本访问经过身份验证的Google云端点API,google-app-engine,authentication,google-apps-script,oauth-2.0,google-cloud-endpoints,Google App Engine,Authentication,Google Apps Script,Oauth 2.0,Google Cloud Endpoints,我正试图从一个使用谷歌云端点构建的API中将一些数据拉入谷歌表单电子表格。以下是API声明: @Api( name = "myendpoint", namespace = @ApiNamespace ( ownerDomain = "mydomain.com", ownerName = "mydomain.com",

我正试图从一个使用谷歌云端点构建的API中将一些数据拉入谷歌表单电子表格。以下是API声明:

@Api(
        name = "myendpoint", 
        namespace = 
            @ApiNamespace
                (
                    ownerDomain = "mydomain.com", 
                    ownerName = "mydomain.com", 
                    packagePath = "myapp.model"
                ),
         scopes = {SCOPES},
         clientIds = {ANDROID_CLIENT_ID, WEB_CLIENT_ID, API_EXPLORER_CLIENT_ID},
         audiences = {WEB CLIENT_ID}
)
@ApiMethod(name = "ping", httpMethod = HttpMethod.GET, path = "ping")
public StringResponse getPing(User user) throws OAuthRequestException {

    CheckPermissions(user);//throws an exception if the user is null or doesn't have the correct permissions

    return new StringResponse("pong");
}
@Api(
        name = "myendpoint", 
        namespace = 
            @ApiNamespace
                (
                    ownerDomain = "mydomain.com", 
                    ownerName = "mydomain.com", 
                    packagePath = "myapp.model"
                ),
         scopes = {SCOPES},
         clientIds = {ANDROID_CLIENT_ID, WEB_CLIENT_ID, API_EXPLORER_CLIENT_ID, GAPPS_CLIENT_ID},
         audiences = {WEB CLIENT_ID}
    )
我试图访问的方法通过API声明中的用户参数启用了身份验证:

@Api(
        name = "myendpoint", 
        namespace = 
            @ApiNamespace
                (
                    ownerDomain = "mydomain.com", 
                    ownerName = "mydomain.com", 
                    packagePath = "myapp.model"
                ),
         scopes = {SCOPES},
         clientIds = {ANDROID_CLIENT_ID, WEB_CLIENT_ID, API_EXPLORER_CLIENT_ID},
         audiences = {WEB CLIENT_ID}
)
@ApiMethod(name = "ping", httpMethod = HttpMethod.GET, path = "ping")
public StringResponse getPing(User user) throws OAuthRequestException {

    CheckPermissions(user);//throws an exception if the user is null or doesn't have the correct permissions

    return new StringResponse("pong");
}
@Api(
        name = "myendpoint", 
        namespace = 
            @ApiNamespace
                (
                    ownerDomain = "mydomain.com", 
                    ownerName = "mydomain.com", 
                    packagePath = "myapp.model"
                ),
         scopes = {SCOPES},
         clientIds = {ANDROID_CLIENT_ID, WEB_CLIENT_ID, API_EXPLORER_CLIENT_ID, GAPPS_CLIENT_ID},
         audiences = {WEB CLIENT_ID}
    )
当使用生成的客户机库或gapi js库时,这可以很好地工作。不过,我不能在应用程序脚本中使用这些js库

我已经使用来自的apps-script-OAuth2库实现了一个OAuth2流,并且我正在使用默认设置创建服务

function getService() {
  // Create a new service with the given name. The name will be used when
  // persisting the authorized token, so ensure it is unique within the
  // scope of the property store.
  return OAuth2.createService(SERVICE_NAME)

  // Set the endpoint URLs, which are the same for all Google services.
  .setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
  .setTokenUrl('https://accounts.google.com/o/oauth2/token')

  // Set the client ID and secret, from the Google Developers Console.
  .setClientId(CLIENT_ID)
  .setClientSecret(CLIENT_SECRET)

  // Set the name of the callback function in the script referenced
  // above that should be invoked to complete the OAuth flow.
  .setCallbackFunction('ruggedAuthCallback')

  // Set the property store where authorized tokens should be persisted.
  .setPropertyStore(PropertiesService.getUserProperties())

  // Set the scopes to request (space-separated for Google services).
  .setScope(SCOPES)

  // Below are Google-specific OAuth2 parameters.

  // Sets the login hint, which will prevent the account chooser screen
  // from being shown to users logged in with multiple accounts.
  .setParam('login_hint', Session.getActiveUser().getEmail())

  // Requests offline access.
  .setParam('access_type', 'offline')

  // Forces the approval prompt every time. This is useful for testing,
  // but not desirable in a production application.
  .setParam('approval_prompt', 'auto')

  //.setParam('include_granted_scopes', 'true');
}
这些是我访问API的方法

function getDriveDocs() {
  return executeApiMethod('https://www.googleapis.com/drive/v2/','files?maxResults=10');
}

function pingServer(){
  return executeApiMethod('https://myapp.appspot.com/_ah/api/myendpoint/v1/','ping');
}

function executeApiMethod(apiUrl, method)
{
  //var url = ;
  var url = apiUrl + method;
  var service = getRuggedService();
  return UrlFetchApp.fetch(url, {
    'muteHttpExceptions': true,
    'method': 'get',
    'headers': {
      Authorization: 'Bearer ' + service.getAccessToken()
    }
  });
}
getDriveDocs()方法工作正常,因此我知道我的身份验证流工作正常。此外,如果我在API中调用未经验证的方法,我会得到正确的响应。但是,当我调用经过身份验证的“ping”方法时,“user”参数始终为null。我在取回电话中遗漏了什么吗?到目前为止,我读到的所有东西似乎都暗示了这种设置

Authorization: 'Bearer ' + service.getAccessToken()
应该足够了


任何帮助都将不胜感激

这原来是一个简单的错误-我在google开发控制台中创建了一个新的oauth2凭据,并且没有将新的客户端id添加到API声明中。以下是工作API声明:

@Api(
        name = "myendpoint", 
        namespace = 
            @ApiNamespace
                (
                    ownerDomain = "mydomain.com", 
                    ownerName = "mydomain.com", 
                    packagePath = "myapp.model"
                ),
         scopes = {SCOPES},
         clientIds = {ANDROID_CLIENT_ID, WEB_CLIENT_ID, API_EXPLORER_CLIENT_ID},
         audiences = {WEB CLIENT_ID}
)
@ApiMethod(name = "ping", httpMethod = HttpMethod.GET, path = "ping")
public StringResponse getPing(User user) throws OAuthRequestException {

    CheckPermissions(user);//throws an exception if the user is null or doesn't have the correct permissions

    return new StringResponse("pong");
}
@Api(
        name = "myendpoint", 
        namespace = 
            @ApiNamespace
                (
                    ownerDomain = "mydomain.com", 
                    ownerName = "mydomain.com", 
                    packagePath = "myapp.model"
                ),
         scopes = {SCOPES},
         clientIds = {ANDROID_CLIENT_ID, WEB_CLIENT_ID, API_EXPLORER_CLIENT_ID, GAPPS_CLIENT_ID},
         audiences = {WEB CLIENT_ID}
    )

这来自定义API并部署到Google云平台的Java文件