2013-05-25 77 views

回答

6

爲ID令牌並執行身份驗證時承載的令牌之間的差的一個長的描述見Google Endpoints API + Chrome Extension returns None for endpoints.get_current_user().user_id()

如果您不使用Android應用程序,則可以自由使用持票人令牌,而不必擔心身份證令牌的某些限制。

接下來get_current_user()oauth庫提供oauth.is_current_user_admin()方法。正如get_current_user(),this method calls_maybe_call_get_oauth_user然後檢查一個簡單的環境變量。

正如在其他答覆中提到:

oauth.get_current_user()電話是隻買貴的IF它使 的RPC。 _maybe_call_get_oauth_user方法存儲最後一次調用的值 ,所以第二次調用oauth.get_current_user()時 將不會導致網絡/速度開銷超過幾納秒至 查找來自Python dict的值。

所以,如果你只使用承載令牌,你可以做以下

from google.appengine.api import oauth 
from google.appengine.ext import endpoints 

... 

endpoints_user = endpoints.get_current_user() 
if endpoints_user is None: 
    raise endpoints.UnauthorizedException(...) 

is_admin = oauth.is_current_user_admin(known_scope) 
if not is_admin: 
    # Throw a 403 FORBIDDEN 
    raise endpoints.ForbiddenException(...) 
相關問題