【问题标题】:Java based Google App Engine, Android and authentication oauth2基于 Java 的 Google App Engine、Android 和身份验证 oauth2
【发布时间】:2014-07-18 14:51:55
【问题描述】:

身份验证和应用程序引擎,有很多值得阅读的内容,但很多似乎已经过时了!

即使是谷歌页面https://developers.google.com/appengine/docs/java/endpoints/consume_android#making-authenticated-calls

在这里,他们谈论“GoogleAccountCredential.usingAudience”,但现在,您应该使用 GoogleAuthUtil(据我所知,如果我错了,请纠正我)。

我正在尝试将应用引擎设置为我的 Android 应用(以及将来我的 iOS 应用)的后端。

我正在使用 Android Studio,使用“新模块”并在那里选择了带有云消息传递的应用引擎。

我创建了一个简单的端点,并在那里有一个函数,这里有一些代码:

public class ReviewEndpoint {

// Make sure to add this endpoint to your web.xml file if this is a web application.

private static final Logger LOG = Logger.getLogger(ReviewEndpoint.class.getName());

/**
 * This method gets the <code>Review</code> object associated with the specified         <code>id</code>.
 * @param id The id of the object to be returned.
 * @return The <code>Review</code> associated with <code>id</code>.
 */
@ApiMethod(name = "getReview")
public Review getReview(@Named("id") Long id) {
    // Implement this function
    Review r = new Review();
    r.setData("test!");

如您所见,这是由 Android Studio 生成的。我实现了一些东西,比如创建“review”对象并在最后返回它。

在 Android 端,我可以这样做:

ReviewEndpoint.Builder b = new ReviewEndpoint.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), null);
ReviewEndpoint ep = b.build();
Review review = ep.getReview(1L).execute();
data = review.getData();

是的,我得到了“测试!” :)

现在,我想对此进行身份验证。我想知道哪个用户写了什么,所以我想我以后会使用 GMail 帐户和 Facebook。

我在这里卡住了。我能够从 Android 上的用户那里获取令牌:

token = GoogleAuthUtil.getToken(MainScreenActivity.this, mAccount.name, "oauth2:https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.profile");

然后您可以将此令牌作为凭据添加到请求中:

Credential cr = new Credential(BearerToken.authorizationHeaderAccessMethod()).setAccessToken(token);
ReviewEndpoint.Builder b = new ReviewEndpoint.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), cr);

然后在应用引擎中我尝试获取用户信息,但如何? 它将作为“承载者”提供吗?我如何获得这个不记名令牌?然后我应该做 API 请求来获取服务器上的数据吗?

这不起作用:

OAuthService service = OAuthServiceFactory.getOAuthService();
try {
    User user = service.getCurrentUser();

谁能提醒我一下?

【问题讨论】:

    标签: android google-app-engine authentication


    【解决方案1】:

    所以,今天,我终于找到了方法!我以前在 Stackoverflow 上对此有过疑问,但从未得到答案,但这些网站给了我答案:

    https://developers.google.com/appengine/docs/java/endpoints/auth

    https://developers.google.com/appengine/docs/java/endpoints/consume_android

    第一个显示了在应用引擎方面需要做的事情。第二页将告诉您如何获取凭据。我非常接近。我不确定是否需要调整第二个链接中提到的 build.gradle 文件。我添加到 App Engine 的内容:

    @Api(name = "reviewEndpoint", version = "v1", ...<<some more stuff here >>
        scopes = {Constants.EMAIL_SCOPE},
        clientIds = {Constants.WEB_CLIENT_ID, Constants.ANDROID_CLIENT_ID},
        audiences = {Constants.ANDROID_AUDIENCE})
    

    然后获取凭据:

    // Initialize the scope using the client ID you got from the Console.
    final String scope = "server:client_id:" + Constants.WEB_CLIENT_ID;
    
    credential = GoogleAccountCredential.usingAudience(activity,scope);
    

    您必须添加用户的电子邮件地址:

    credential.setSelectedAccountName("some-mail-address@gmail.com");
    

    您可以使用帐户选择器获取电子邮件地址(点击链接时也会显示示例)

    接下来。您使用凭据调用端点,我认为 Play Services 将验证用户,因为如果我使用未在设备上登录的电子邮件,它将无法正常工作。以下代码将引发 GoogleAuthIOException :

    ReviewEndpoint.Builder b = new ReviewEndpoint.Builder(
        AndroidHttp.newCompatibleTransport(), 
        new AndroidJsonFactory(), id_token);
    ReviewEndpoint ep = b.build();
    Review review;
    review = ep.getReview(1L).execute();
    

    为了进行测试,我将在服务器端获得的电子邮件地址作为字符串放入 review 对象中,它给了我电子邮件地址而不是用户对象为空。哦!我忘了告诉你,你在应用引擎端需要一个用户参数。即使您在上面的“getReview”调用中看不到“user”参数,App Engine 也会添加它。

    这就是我的 getReview 现在的样子:

    @ApiMethod(name = "getReview")
    public Review getReview(@Named("id") Long id, User user) {
        // Implement this function
        Review r = new Review();
    
        r.setData("user == " + (user == null ? "NULL " : user.toString()));
    

    希望这会对某人有所帮助

    【讨论】:

    • 你好@Boy,我正在使用OAuthService service = OAuthServiceFactory.getOAuthService();使用弹簧安全性。我已经完成了您提到的相同步骤,但是尽管有 id_token,但我仍然在传递 GoogleAccountCredential 对象这个 OAuthService 服务 = OAuthServiceFactory.getOAuthService();尝试 { 用户用户 = service.getCurrentUser();不工作。请帮忙。
    • 很抱歉,已经快 3 年了,我还没有进一步研究这个问题。我对此不够熟悉,无法进一步帮助您:(
    猜你喜欢
    • 2011-04-23
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多