3

我在Android上使用Google Play服務訪問Google Apis和Google雲端點。我也可以使用Google Play服務中的令牌訪問appengine User API。這可能嗎?有關OAuth的link有一些示例代碼,但有點含糊。我可以通過標題中的oauth標記並獲取下面代碼的用戶?Oauth在Appengine上使用Google Play服務

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. 
     // ... 
    } 

回答

6

可以,但這種方法不會是因爲你在哪裏方案爲安全:使用特定服務範圍爲谷歌API

  • 訪問的端點

    • API直接

    您可以使用Google Play服務來obtain a token作爲您想要使用的範圍。通過授權頭

    String mScope = "https://www.googleapis.com/auth/userinfo.email"; 
    try { 
        token = GoogleAuthUtil.getToken(mActivity, mEmail, mScope); 
    } catch { 
        ... 
    } 
    

    發送給應用程序引擎::既然你有興趣使用App Engine上Users API,你會希望userinfo.email範圍

    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有其他邏輯來防止這種行爲,這就是爲什麼您最好使用其中一種方法。

  • +0

    感謝您的回覆,它幫助我清除了一些東西。我絕對使用GS +端點方法。 – Patrick

    +0

    Hi @Dan,我在服務器端用sprint Oauth2身份驗證GS +端點,我得到「完全授權是必需的」。你有什麼想法嗎? –