Boy*_*Boy 1 authentication google-app-engine android
身份验证和应用程序引擎,有很多东西需要阅读,但很多似乎已经过时了!
在这里,他们谈论'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!");
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,这是由Android Studio很好地生成的.我实现了一些stuf,比如创建'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();
Run Code Online (Sandbox Code Playgroud)
是的,我得到'测试!' :)
现在,我希望对此进行身份验证.我想知道哪个用户写了什么,所以我想我以后会使用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");
Run Code Online (Sandbox Code Playgroud)
然后您就可以将此令牌作为凭证添加到请求中:
Credential cr = new Credential(BearerToken.authorizationHeaderAccessMethod()).setAccessToken(token);
ReviewEndpoint.Builder b = new ReviewEndpoint.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), cr);
Run Code Online (Sandbox Code Playgroud)
然后在应用程序引擎中我试图获取用户信息,但是如何?它会作为"持票人"提供吗?我如何获得此持有人令牌?我应该做API请求来获取服务器上的数据吗?
这不起作用:
OAuthService service = OAuthServiceFactory.getOAuthService();
try {
User user = service.getCurrentUser();
Run Code Online (Sandbox Code Playgroud)
谁能给我一个抬头?
最后,今天,我发现了如何做到这一点!之前我在Stackoverflow上有过这方面的问题而且从来没有得到过答案,但这些网站给了我答案:
https://developers.google.com/appengine/docs/java/endpoints/auth
https://developers.google.com/appengine/docs/java/endpoints/consume_android
第一部分展示了在app引擎端需要完成的工作.第二页将告诉您如何获取凭据.我很亲密.我不确定是否需要调整第二个链接中提到的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})
Run Code Online (Sandbox Code Playgroud)
然后获取凭据:
// 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);
Run Code Online (Sandbox Code Playgroud)
您必须添加用户的电子邮件地址:
credential.setSelectedAccountName("some-mail-address@gmail.com");
Run Code Online (Sandbox Code Playgroud)
您可以使用帐户选择器获取电子邮件地址(也可以在您点击链接时显示示例)
接下来.你使用凭证调用端点,我认为Play服务将验证用户,因为如果我使用未登录设备的电子邮件,它将无法正常工作.以下代码将抛出GoogleAuthIOException:
ReviewEndpoint.Builder b = new ReviewEndpoint.Builder(
AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), id_token);
ReviewEndpoint ep = b.build();
Review review;
review = ep.getReview(1L).execute();
Run Code Online (Sandbox Code Playgroud)
为了测试,我把我在服务器端获得的电子邮件地址作为一个字符串放在审阅对象中,然后它给了我电子邮件地址而不是用户对象为空.噢!我忘了告诉你,你需要在app引擎端有用户参数.即使您在上面的'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()));
Run Code Online (Sandbox Code Playgroud)
希望这会对某人有所帮助
| 归档时间: |
|
| 查看次数: |
1268 次 |
| 最近记录: |