【发布时间】: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 云端点文档,我被定向到:
- 配置我的 Android 客户端(我的 android 应用)以向服务对象提供凭据
- 设置帐户选择器以支持用户选择登录帐户。
我按照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