![](/img/trans.png)
[英]How can I access Google App Engine endpoints API from Python application with use OAuth?
[英]How do I use the Google API Explorer to test my own App Engine Endpoints using OAuth?
我在App Engine上部署了Endpoints API。 我使用Google API資源管理器向不需要登錄的API方法發出請求沒有問題。我使用的URL是:
https://developers.google.com/apis-explorer/?base=https://[MY_APP_ID].appspot.com/_ah/api
我遇到的問題是調用需要用戶登錄的API方法,例如:
@ApiMethod(name = "config.get",
clientIds = {"[MY_CLIENT_ID].apps.googleusercontent.com", "com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID"},
audiences = {"[MY_APP_ID].appspot.com"},
scopes = {"https://www.googleapis.com/auth/userinfo.email"})
public Config getConfig(User user) throws OAuthRequestException {
log.fine("user: " + user);
if (user == null) {
throw new OAuthRequestException("You must be logged in in order to get config.");
}
if (!userService.isUserAdmin()) {
throw new OAuthRequestException("You must be an App Engine admin in order to get config.");
}
...
在API資源管理器上有一個右上角的開關,當單擊它時,允許我指定范圍和授權。 我只是檢查了userinfo.email范圍。 這沒什么區別。 我從電話中得到的回應是:
503 Service Unavailable
- Show headers -
{
"error": {
"errors": [
{
"domain": "global",
"reason": "backendError",
"message": "java.lang.IllegalStateException: The current user is not logged in."
}
],
"code": 503,
"message": "java.lang.IllegalStateException: The current user is not logged in."
}
}
當Endpoints處於Trusted Tester階段時,我記得在OAuth2 Playground中有一個手動步驟來獲取ID令牌而不是訪問令牌或某些此類東西。 如果仍然需要,那么現在任何提及它的內容似乎都已從Endpoints文檔中消失了,我現在看到了在API Explorer中交換令牌的方法。
我看到你的引號中有"com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID"
。 如果這不是你的Stack Overflow轉錄中的拼寫錯誤,那就是一個問題。 該值已經是一個字符串,因此您只需將文本com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID
(不是實際的客戶端ID)作為白名單范圍傳遞。 那不行。 試試這個:
@ApiMethod(name = "config.get",
clientIds = {"[MY_CLIENT_ID].apps.googleusercontent.com", com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID},
audiences = {"[MY_APP_ID].appspot.com"},
scopes = {"https://www.googleapis.com/auth/userinfo.email"})
編輯 :端點內不支持isUserAdmin
,可能是導致錯誤的次要原因。 我建議在提供的User對象上提交支持此方法的功能請求(我們可能不會為用戶服務本身提供支持,因此它與OAuth登錄分開。)
我不知道什么時候引入,但是如果你使用OAuth2而不是UserService.isUserAdmin()
你可以使用OAuthServiceFactory.getOAuthService().isUserAdmin(EMAIL_SCOPE)
,其中EMAIL_SCOPE
是“ https://www.googleapis.com/ auth / userinfo.email “。
這使得使用舊的OpenId或OAUth2變得容易:
boolean isAdmin = false;
try {
isAdmin = userService.isUserAdmin());
} catch (IllegalStateException e1) {
try {
isAdmin = OAuthServiceFactory.getOAuthService().isUserAdmin(EMAIL_SCOPE);
} catch (Exception e2) {}
}
幾年前問過原來的問題,但也許這會對其他人有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.