gpt4 book ai didi

java - Google端点java Oauth/Oauth2完整示例

转载 作者:行者123 更新时间:2023-11-30 02:13:19 25 4
gpt4 key购买 nike

看了很多链接后:我无法解决有关服务器端 getuser=null 问题的解决方法。我需要一个完整的示例服务器端(app engine java)和客户端(android app)。在某些链接中,人们说:将用户保存到数据存储区并将其恢复。但我无法保存用户...因为它始终为空...

package com.example.ismab_000.myapplication.backend;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;
import com.google.appengine.api.oauth.OAuthRequestException;
import com.google.appengine.api.oauth.OAuthService;
import com.google.appengine.api.oauth.OAuthServiceFactory;
import com.google.appengine.api.users.User;

import javax.inject.Named;

/**
* An endpoint class we are exposing
*/
@Api( name = "myApi",
version = "v1",
scopes = {Constants.EMAIL_SCOPE},
clientIds = {Constants.WEB_CLIENT_ID, Constants.ANDROID_CLIENT_ID,Constants.IOS_CLIENT_ID},
audiences = {Constants.ANDROID_AUDIENCE},
namespace = @ApiNamespace(ownerDomain = "backend.myapplication.ismab_000.example.com", ownerName = "backend.myapplication.ismab_000.example.com", packagePath = ""))

public class MyEndpoint {

User user = null;
MyBean response = new MyBean();
String email;

/**
* A simple endpoint method that takes a name and says Hi back
*/

@ApiMethod(name = "sayHi")
public MyBean sayHi(@Named("name") String name) {
/*
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.
// ...
response.setData("Hola desde Metodo1, " + name);
}

*/
email= user.getEmail();

//response.setData("Hi, " + name + user.getEmail().toString());
response.setData("Hi, desde APYSAYHI...");
return response;
}
@ApiMethod(name = "Metodo1")
public MyBean metodo1(@Named("name") String name) {

response.setData("Hola desde Metodo1, " + name);
return response;
}
@ApiMethod(name = "Metodo2")
public MyBean metodo2(@Named("name") String name) {

response.setData("Hola desde Metodo2, " + name);
return response;
}


}

问候。

最佳答案

为了访问用户对象,您首先需要配置端点以使用 OAuth。说明可在此处找到:https://cloud.google.com/appengine/docs/java/endpoints/auth

OAuth 意味着您需要将项目配置为在定义 API 时引用的常量对象下配置正确的客户端 ID。

然后您需要将 User 对象添加到 Endpoint 方法中,例如:

@ApiMethod(name = "sayHi")
public MyBean sayHi(@Named("name") String name, User user) throws OAuthRequestException, IOException {

If an incoming client request has no authorization token or an invalid one, user is null. In your code, you need to check whether user is null and do ONE of the following, depending on the condition:

  • If the user is non-null, perform the authorized action.
  • If the user is null, throw an OAuthRequestException.
  • Alternatively, if the user is null, perform some action for an unauthorized client access if some sort of unauthorized access is
    desired.

关于java - Google端点java Oauth/Oauth2完整示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29797131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com