【问题标题】:Authenticating my android client to connect to an endpoint验证我的 android 客户端以连接到端点
【发布时间】:2015-03-23 18:04:55
【问题描述】:

我正在为一家在线报纸公司开发一个安卓应用程序。该公司已经在支持 OAuth 2.0 的 Google App Engine 上开发和托管了他们的 API。
因此,我要开发一个 Android 应用程序,该应用程序与他们部署的支持 OAuth 2.0 的后端 API 进行通信,并从他们分配的 Google API Explorer 中获取内容响应。

因此,根据 android 客户端(如我的应用程序)尝试通过 OAuth 2.0 支持对端点进行经过身份验证的调用的 Google 云端点文档,我被定向到:

  1. 配置我的 Android 客户端(我的 android 应用)以向服务对象提供凭据
  2. 设置帐户选择器以支持用户选择登录帐户。

我按照Google Cloud Endpoint网站上的说明进行操作,但我不明白用于解释的示例代码,所以我尝试了这个:

class EndpointsAsyncTask extends AsyncTask<Void, Void, Void>{

       @Override
       protected Void doInBackground(Void... params) {

           HttpTransport httpTransport = new NetHttpTransport();
           JsonFactory jsonFactory = new  AndroidJsonFactory();


           try {
               GoogleCredential credential = new GoogleCredential.Builder()
                       .setTransport(httpTransport)
                       .setJsonFactory(jsonFactory)
                       .setServiceAccountId(CLIENT_EMAIL)
                       .setServiceAccountScopes(Collections.singleton("https://www.googleapis.com/auth/userinfo.email"))
                       .setServiceAccountPrivateKeyFromP12File(new File("file.p12"))
                       .build();

           /*so now what do I do with the credential object 
          and how do I set the root URL (https://project-id-endpoints.appspot.com/_ah/api)
          that the android client(this android app)
         will connect to in the backend API call
         */

           }
           catch(GeneralSecurityException gse){
               gse.printStackTrace();
           }
           catch(IOException ioe){

           }


           return null;
       }

       @Override
       protected void onPostExecute(Void aVoid) {
           super.onPostExecute(aVoid);

       }
   }

每当我运行我的代码时,Google 开发者控制台上的日志报告都会显示

“未授权访问”表示身份验证调用不起作用...

以下是我打开与 API 服务内容之一的 GET URL 连接的代码:

public String open() throws IOException{

        InputStream inputStream = null;
        int len = 500;

        try {
            URL url = new URL("https://project-name-api-endpoints.appspot.com/_ah/api/core/v5.1.1/info/category/en");

            URLConnection urlConnection = url.openConnection();

            inputStream = urlConnection.getInputStream();

            String content = readIt(inputStream, len);

            return content;
        }//end try

        finally {
            if(inputStream != null)
            {
                inputStream.close();
            }
        }

    }

我的问题是:

如何处理凭证对象以及如何设置 android 客户端(此 android 应用程序)将在后端 API 调用中连接的根 URL (https://project-id-endpoints.appspot.com/_ah/api)

【问题讨论】:

  • 我的问题是……我不明白我共享链接的 google 文档……我需要帮助将我的 android 应用程序连接到 google api 端点
  • 听起来您想将现有的 App Engine 项目变成一个 Android 应用程序连接的 Cloud Endpoint,对吗?
  • "我不理解我分享链接的谷歌文档" 我们无法真正解释问答网站上的内容是如何工作的。您需要继续阅读,直到您理解为止,在论坛中提问(更欢迎讨论)或在此处发布有关您正在努力解决的代码的特定部分的更明确的问题。
  • 我已经重新整理了我的问题,希望能得到更好的理解和回答。谢谢...@rene
  • @LynnCrumbling 我已经重新调整了我的问题......请再次检查......谢谢

标签: android google-app-engine google-cloud-endpoints google-api-client urlconnection


【解决方案1】:

问题是您在调用端点时没有使用您设置的那些凭据。您正在建立的连接完全不知道您从端点 + Android 获得的所有 OAuth 设置和精彩内容。

理想情况下,您需要做的是:

1) 从您的端点创建适用于 Android 的客户端库(如 here 所述)。不要对您的端点进行“手动”(通过 URLConenction)调用,尽管从技术上讲这是可能的,但绝对不建议这样做。

2) 使用项目中包含的这些库(jar),您只需调用所需的任何方法(如here 所述),这些库将根据您的应用程序设置包含所有必需的授权标头等。您无需担心有关 OAuth 的其他任何事情。

提示:确保在 Google 控制台的授权中包含您开发者的所有 SHA 调试签名。如果不这样做,您将只能从“生产”应用调用端点。

【讨论】:

  • 好的,客户端库已经生成。但是根据这个link,我是否还需要将模板端点模块添加到我的项目中,或者如何将这个客户端库添加到我的项目中......因为我的客户端库是.zip格式(后端API是使用开发的蟒蛇)
  • 我一直使用 Java 作为后端(并直接获取一个 jar 文件),但一项快速研究表明您应该解压缩该文件并将包含的库导入 Android Studio。
  • 解压后的文件夹中没有库...有关我的问题的详细问题,请参阅此link
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-24
  • 2011-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多