基于Java的谷歌应用程序引擎,Android和认证oauth2 [英] Java based Google App Engine, Android and authentication oauth2

查看:217
本文介绍了基于Java的谷歌应用程序引擎,Android和认证oauth2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

身份验证和应用程序引擎,还有很多有待了解,但很多似乎已经过时!

Authentication and app engine, there is a lot to be read about it, but a lot seems to be outdated!

即使是谷歌的网页<一href="https://developers.google.com/appengine/docs/java/endpoints/consume_android#making-authenticated-calls" rel="nofollow">https://developers.google.com/appengine/docs/java/endpoints/consume_android#making-authenticated-calls

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

Here, they talk about 'GoogleAccountCredential.usingAudience', but nowadays, you should use GoogleAuthUtil (as far as I know, please correct me if I'm wrong).

我想建立一个应用程序引擎作为后端到我的Andr​​oid应用程序(和未来,我的iOS应用)。

I am trying to set up an app engine as a backend to my Android app (and in future, my iOS app).

我使用的是Android Studio中,使用了新模块,并选择了App Engine的云消息出现。

I am using Android Studio, used the 'new module' and chose app engine with cloud messaging there.

我创建了一个简单的端点,并有一个功能存在,这里是一些code:

I created a simple endpoint, and have a function there, here is some code:

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生成。我实现了一些STUF如创建了审查对象,并在年底返回。

As you can see, this is nicely generated by Android Studio. I implemented some stuf like creating the 'review' object and return it at the end.

在Android的一面,我可以做到这一点:

On the Android side, I can do this:

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

是的,我得到'的考验! :)

and yes, I get 'test!' :)

现在,我想有这个认证。我想知道哪些用户说了什么,所以我想我会使用Gmail帐户和Facebook之后。

Now, I want to have this authenticated. I want to know which user wrote what, so I thought I am going to use GMail account and Facebook later.

在这里,我坚持。我能够从用户那里得到一个令牌上的Andr​​oid:

Here I'm stuck. I am able to get a token from the user on Android:

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

,那么你可以添加此令牌作为凭证请求:

then you are able to add this token as credential to the request:

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

然后在应用程序引擎我试图获得用户信息,但如何? 它是否将提供作为载体?我如何获得这个承载令牌?如果我再做API请求获取服务器上的数据?

Then in the app engine I tried to get the user info, but how? Will it be supplied as 'bearer'? How do I get this bearer token? Should I then do API request to get the data on the server?

这不工作:

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

谁能给我一个头了?

can anyone give me a heads up?

推荐答案

所以最后,今天,我发现了如何做到这一点!我对#1之前对这个问题,从来没有一个答案,但这些的网站给我的回答:

So finally, today, I found out how to do it! I had questions on Stackoverflow on this before and never had an answer, but these to sites gave me the answer:

<一个href="https://developers.google.com/appengine/docs/java/endpoints/auth">https://developers.google.com/appengine/docs/java/endpoints/auth

<一个href="https://developers.google.com/appengine/docs/java/endpoints/consume_android">https://developers.google.com/appengine/docs/java/endpoints/consume_android

第一条显示什么需要的应用程序引擎一侧进行。第二页会告诉你如何得到的凭据。我很接近。我不知道,如果在第二个链接中提到的build.gradle文件的调整是必要的。我加入到App Engine的:

The first shows what needs to be done on the app engine side. The second page will tell you how to get the credentials. I was quite close. I am not sure if the adjusting of the build.gradle file mentioned in the second link is necessary. What I added to the 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})

然后拿到凭证:

and then get the credentials:

// 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);

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

You have to add the e-mail address of the user:

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

您可以使用该帐户选取器获得的电子邮件地址(也例如,当您按照链接显示)

you can get the e-mail address using the account picker (also example shown when you follow the link)

和下一个。你做一个呼叫端点,使用的凭证,我想播放服务将会验证用户,因为如果我使用的电子邮件未记录在设备上,它不会工作。下面code将抛出一个GoogleAuthIOException:

and next. you do a call to the endpoint, using the credential, I think Play Services will validate the user, because if I use an e-mail that is not logged in on the device, it will not work. The following code will throw an GoogleAuthIOException :

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

有关测试,我已经把E-mail地址,我得到在服务器端以字符串形式审查的对象,有它给我的电子邮件地址,而不是为空的用户对象。噢!我忘了告诉你,你需要的应用程序引擎端用户的说法。即使你没有看到在上面的getReview电话用户的说法,它将应用程序引擎被加入。

for testing, I've put the e-mail address I get at the server side as a string in the review object, and there it gave me the e-mail address instead of the user object being null. Ow! I forgot to tell you, you need a user argument on the app engine side. Even though you do not see the 'user' argument in the 'getReview' call above, it will be added by App Engine.

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

So this is how my getReview looks now:

@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()));

希望这会帮助别人

Hope this will help someone

这篇关于基于Java的谷歌应用程序引擎,Android和认证oauth2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆