我正在尝试将一些数据从使用Google Cloud Endpoints构建的API中提取到Google表格电子表格中 . 这是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}
)
我尝试访问的方法通过API声明中的user参数启用了身份验证:
@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");
}
这在使用生成的客户端库或gapi js库时工作正常 . 但是AFAIK我不能在Apps脚本中使用那些js库 .
我使用来自here的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()方法工作正常,所以我知道我的auth流程正常工作 . 此外,如果我在API中调用未经身份验证的方法,我会得到正确的响应 . 但是,当我调用经过身份验证的“ping”方法时,“user”参数始终为null . 我在fetch调用中遗漏了什么吗?到目前为止,我所阅读的所有内容似乎都暗示了这一点
Authorization: 'Bearer ' + service.getAccessToken()
应该够了 .
任何帮助将非常感激!
1 回答
这被证明是一个简单的错误 - 我在google开发者控制台中创建了一个新的oauth2凭证,并且没有将新的客户端ID添加到API声明中 . 这是工作API声明: