[英]Oauth on Appengine with Google Play services
我在Android上使用Google Play服务来访问Google Apis和Google云端点。 我还可以使用Google Play服务中的令牌访问appengine User api。 这可能吗? OAuth的这个链接有一些示例代码,但有点模糊。 我可以在标题中传递oauth令牌并获取下面代码的用户吗?
User user = null;
try {
OAuthService oauth = OAuthServiceFactory.getOAuthService();
user = oauth.getCurrentUser();
} catch (OAuthRequestException e) {
// The consumer made an invalid OAuth request, used an access token that was
// revoked, or did not provide OAuth information.
// ...
}
您可以,但这种方法不会像您所处的场景一样安全:
您可以使用Google Play服务获取您要使用的范围的令牌 。 由于您对在App Engine上使用Users
API感兴趣,因此您需要userinfo.email
范围:
String mScope = "https://www.googleapis.com/auth/userinfo.email";
try {
token = GoogleAuthUtil.getToken(mActivity, mEmail, mScope);
} catch {
...
}
通过Authorization标头将其发送到App Engine:
Authorization: Bearer your_token
然后,使用OAuth
API,您可以获取User
对象:
String mScope = "https://www.googleapis.com/auth/userinfo.email";
User user = null;
try {
OAuthService oauth = OAuthServiceFactory.getOAuthService();
user = oauth.getCurrentUser(mScope);
} catch (OAuthRequestException e) {
// The consumer made an invalid OAuth request, used an access token that was
// revoked, or did not provide OAuth information.
// ...
}
但是,一般来说你不想这样做! 如果以这种方式保护您的应用程序,另一个用户可以编写一个应用程序,要求您授权userinfo.email
范围。 一旦被授予,他们需要做的就是获取令牌并将其传递给您的应用程序,它们就像您一样出现。 端点和其他Google API具有额外的逻辑来防止这种行为,这就是为什么你最好使用这些方法之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.