【发布时间】:2014-07-18 14:51:55
【问题描述】:
身份验证和应用程序引擎,有很多值得阅读的内容,但很多似乎已经过时了!
在这里,他们谈论“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