[英]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.