Google 云端点(应用程序引擎)+ oauth2 与 android 集成

2024-04-23

我正在尝试将谷歌应用程序引擎云端点 API 与 android 集成。

我已经按照此链接进行了相同的操作:

  1. 无法使用我的服务对象连接到我的谷歌端点 https://stackoverflow.com/questions/29544723/unable-to-connect-to-my-google-endpoints-with-my-service-object

  2. https://cloud.google.com/appengine/docs/java/endpoints/consume_android https://cloud.google.com/appengine/docs/java/endpoints/consume_android

  3. 基于 Java 的 Google App Engine、Android 和身份验证 oauth2 https://stackoverflow.com/questions/24827919/java-based-google-app-engine-android-and-authentication-oauth2

这是我的 Android 应用程序端代码和应用程​​序引擎端点代码:

**<Main activity>**

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Profile profile = new Profile();
    profile.setDisplayName("Alex roger");
    profile.setAge(49l);
    new EndpointsAsyncTask(this).execute(profile);
} 

**<EndpointAsyncTask>**

public class EndpointsAsyncTask extends AsyncTask<Profile, Void, Profile> {
private MyApi myApiService;
private Context context;
SharedPreferences settings;
GoogleAccountCredential credential;
private static final String PREF_ACCOUNT_NAME = "PREF_ACCOUNT_NAME";

public EndpointsAsyncTask(Context context) {
    this.context = context;
}

@Override
protected Profile doInBackground(Profile... params) {
    Profile profile = params[0];
    try {
            settings = context.getSharedPreferences(PREF_ACCOUNT_NAME, Context.MODE_PRIVATE);
            credential = GoogleAccountCredential.usingAudience(context,
                "server:client_id:<web-client-id>.apps.googleusercontent.com");
            credential.setSelectedAccountName("<myemail_logged_in>@gmail.com");

            MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
                    new AndroidJsonFactory(), credential);
            builder.setApplicationName(context.getPackageName());
            builder.setRootUrl("https://<my-app-id>.appspot.com/_ah/api/");
            myApiService = builder.build();

            return myApiService.saveProfile(profile.getAge(), profile.getDisplayName()).execute();

     }
     catch (IOException e) {
        Log.e("IOException", String.valueOf(e.getCause() +" " +e));
    } 
     catch (Exception e) {
        Log.e("Exception", e+"");
    }
    return profile;
}

@Override
protected void onPostExecute(Profile result) {
    Log.e("Display Name", result.getDisplayName());
}

}

**<MyEndpoint>**

 @Api(
    name = "myApi",
    version = "v1",
    namespace = @ApiNamespace(
            ownerDomain = "backend.endpoint",
            ownerName = "backend.endpoint",
            packagePath = ""
    ),
    scopes = {Constant.API_EMAIL_SCOPE},
    clientIds = {"<web-client-id>.apps.googleusercontent.com",
            "<android-client-id>.apps.googleusercontent.com",
            Constant.API_EXPLORER_CLIENT_ID},
    audiences = {"<web-client-id>.apps.googleusercontent.com"}
)
public class MyEndpoint {


@ApiMethod(name = "saveProfile",path = "saveProfile", httpMethod = ApiMethod.HttpMethod.POST)
public Profile saveProfile(@Named("name") String name, @Named("age") long age, User user) {
    Profile profile = new Profile(name,age);
    ofy().save().entity(profile).now();

    return profile;
}

@ApiMethod(name = "saveProfile1",path = "saveProfile1", httpMethod = ApiMethod.HttpMethod.POST)
public Profile saveProfile1(@Named("name") String name, @Named("age") long age) {
    Profile profile = new Profile(name,age);
    ofy().save().entity(profile).now();

    return profile;
}
}

**<Profile.class>**

@Entity
@Cache
public class Profile {

String displayName;
@Id
long age;

private Profile(){}
public Profile(final String displayName,final long age){
    this.displayName = displayName;
    this.age = age;
}
public String getDisplayName()
{
    return displayName;
}
public  long getAge()
{
    return age;
}

}

**<OfyService>**

public class OfyService {

static {
    factory().register(Profile.class);
}

public static Objectify ofy() {
    return ObjectifyService.ofy();
}

public static ObjectifyFactory factory() {
    return ObjectifyService.factory();
}
}

但出现错误“IOException:com.google.android.gms.auth.GoogleAuthException:未知com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAuthIOException”。

如果我们不使用 oAuth2 服务(用户用户),那么它可以正常工作。还建议一些有用的链接。


问题已解决:使用错误的 SHA1 创建 android-client-id。 此链接帮助我发现了我的错误:

"Google 端点 - Android GoogleAuthIOException Tic Tac Toe - 删除了 clientIds https://stackoverflow.com/questions/21926709/google-endpoints-android-googleauthioexception-tic-tac-toe-removed-clientids"

注意:debug.keystore 文件可以在 c:\Users\ .android\ 中找到

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Google 云端点(应用程序引擎)+ oauth2 与 android 集成 的相关文章

随机推荐

  • 如何使用 jquery 旋转悬停图像?

    我试图在悬停时将 返回顶部 按钮旋转 360 度 而在鼠标离开时不取消旋转 我已经尝试了多种 jQuery 代码变体 但我似乎仍然无法让它工作 这是迄今为止我所得到的真实示例 CSS 悬停在图像之间 我尝试将 jQuery 更改为mouse
  • Rails 3:更改现有 mysql 数据库的字符集和排序规则

    是否可以使用 Rails 迁移或其他选项更改现有 Mysql 数据库的字符集和排序规则 初始配置数据库字符集和排序规则的最佳方法是什么 本机查询可以在 Rails 迁移中执行 def self up execute ALTER DATABA
  • 如何使用linq动态过滤子集合

    我正在尝试过滤用户请求的结果 例如你有orders and order details and products是子集合 当用户想要按产品过滤时 我收到错误 因为No property or field PRODUCTS exists in
  • 如何在 FastAPI 中访问端点视图函数内的 APP 属性?

    这是我的项目结构 gitignore README md requirements txt start py app main py apis v1 init py routes evaluation essentials py train
  • python pandas 选择头部和尾部

    对于 Pandas 中的 DataFrame 如何同时选择前 5 个值和后 5 个值 例如 In 11 df Out 11 A B C 2012 11 29 0 0 0 2012 11 30 1 1 1 2012 12 01 2 2 2 2
  • 无法将“obj\Debug\{project}.dll”复制到“bin\{project}.dll”

    最近 当我尝试运行我的项目时 Web 版 Visual Studio Express 2013 经常抛出此错误 我找到的唯一解决方案是退出并重新启动 Visual Studio 或 有时 完全重新启动 Windows 什么会导致这样的事情
  • 加载时css3过渡动画?

    是否可以在页面加载时使用 CSS3 过渡动画而不使用 Javascript 这就是我想要的 但是在页面加载时 图像滑块 html https web archive org web 20141021062316 http rilwis go
  • Spring Boot x509 测试 - pcf

    In 云铸造厂我已对其进行配置 以便将客户端证书转发到我的 Spring Boot 应用程序 该证书被放置在x forwarded client certheader 中 spring boot 应用程序读取 this 并检查 CN 是否已
  • 如何在access中查看宏代码?

    我有一个 Microsoft Access 数据库 里面有一个宏 如何查看宏的代码 打开Access数据库 您将看到表 查询 报告 模块和宏 其中包含可用于按顺序调用常见 MS Access 操作的宏 对于自定义 VBA 宏 请按 ALT
  • Safari 在 div 中使用 Google 地图打破边框半径

    对我来说关于 Stack 的第一个问题 我已经完成了我的作业并发现了类似这个主题的内容 在 webkit 浏览器中 v3 谷歌地图不尊重容器的边框半径 有人有解决方法吗 https stackoverflow com questions 1
  • 查找API端点的方法

    API探索 尽管有几个问题涉及该主题 但我找不到解决我想要理解的核心概念的问题 如果知道 API 的根结构 我们可以想象一下http stackoverflow com api service 我们可以成功地从已知端点检索结果 比方说htt
  • 如何安全地销毁 QThread?

    我想正确地销毁一个QThread在 Qt 5 3 中 到目前为止我已经得到 MyClass MyClass QObject parent QObject parent mThread new QThread this QObject con
  • 获取 3 个列表之间的差异

    我正在研究列表的差异 gt gt a 1 2 3 gt gt b 2 4 5 gt gt c 3 2 6 两组之间的对称差异可以使用以下方法完成 gt gt z set a symmetric difference set b gt gt
  • 3D 空间中的激光投影仪校准

    我正在研究一种在现实世界中校准激光投影仪的解决方案 该项目有几个目标 1 Take in a minimum of four points measured in the real world in 3d space that repres
  • dojo.parser.parse 仅在第一次调用时工作

    我有一个页面 当用户单击某些报告工具的链接时 它首先要求他们输入一些报告参数 我根据链接的 id 使用 AJAX 以表单形式获取参数对话框 每个对话框上都有一些 dojo 控件 因此我需要在对话框出现时解析它们 因为它最初不是页面的一部分
  • 可以在 Mathematica 中扩展 PDF、CDF、FindDistributionParameters 等功能吗?

    我开始使用新的 Mathematica 统计和数据分析功能进行越来越多的工作 我周二参加了 Mathematica 统计与数据分析 在线研讨会 非常棒的演讲 我强烈推荐 但我遇到了一些问题 我希望这个论坛上的人可以花点时间考虑一下 我创建了
  • VSO/VSTS:在主机代理上使用发布构建工件时出错

    使用 发布构建工件 任务时 构建中会引发以下错误 就在 获取源 之后 任务的定义没有与任何匹配的处理程序 由该代理支持 支持的处理程序 AzurePowerShell AzurePowerShellHandler PowerShell Po
  • 创建会话[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我需要在 php 中创建会话的帮助 我需要一个登录会话 然后如果用户想更改他们的用户名 他们可以但是 我知道如何更改用户名 但使用会话时情
  • 如何在 React App 完成加载之前显示全页加载器/旋转器

    如何制作全页加载器 旋转器 首先加载 然后显示 直到 React 或不同的基于 JS 的框架 应用程序完全加载 完全加载 是指浏览器旋转器停止旋转的那一刻 我正在为非 js 渲染的网站制作这些加载器 旋转器 但我不确定如何为 JS 渲染的应
  • Google 云端点(应用程序引擎)+ oauth2 与 android 集成

    我正在尝试将谷歌应用程序引擎云端点 API 与 android 集成 我已经按照此链接进行了相同的操作 无法使用我的服务对象连接到我的谷歌端点 https stackoverflow com questions 29544723 unabl