2013-05-25 75 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(...) 
相关问题