该方法setJsonHtt prequestInitializer是未定义的类型Drive.Builder的Andr​​oid [英] The method setJsonHttpRequestInitializer is undefined for the type Drive.Builder Android

查看:190
本文介绍了该方法setJsonHtt prequestInitializer是未定义的类型Drive.Builder的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个应用程序与谷歌Android版云端硬盘的工作。

I am creating an application to working with Google Drive on Android.

我搜索互联网的几个例子,但他们都是相同的。我得到了以下错误:

I've searched in internet for a few examples, but they all are the same. I'm getting the following error:

Object Drive.Builder does not have method setJsonHttpRequestInitializer.

我已经与谷歌驱动API V1和V2没有运气测试。

I have tested with Google drive API V1 and V2 without luck.

问题出在哪里?

来源:

private Drive getDriveService(String accountName) {

   HttpTransport ht = AndroidHttp.newCompatibleTransport();                     
   JacksonFactory jf = new JacksonFactory();          
   Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod()).setAccessToken(accountName);           

        Drive.Builder b = new Drive.Builder(ht, jf, null);
        b.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {

            @Override
            public void initialize(JsonHttpRequest request) throws IOException {
                DriveRequest driveRequest = (DriveRequest) request;
                driveRequest.setPrettyPrint(true);
                driveRequest.setOauthToken(accountName);
                driveRequest.setKey(API_KEY);
            }
        });
        return b.build();
} 

发现,如果使用setJsonHtt prequestInitializer方法是不确定的谷歌API的Java的客户端1.12.0-β

Found that setJsonHttpRequestInitializer method is undefined if use google-api-java-client-1.12.0-beta

下载和进口谷歌的API-Java的客户端1.11.0-β

Downloaded and imported google-api-java-client-1.11.0-beta

但现在越来越:
使用客户端ID为安装的应用程序:客户端ID

But now getting: Using Client ID for installed applications: Client ID.

com.google.api.client.http.HttpResponseException: 403 Forbidden
 {
  "error": {
   "errors": [
    {
     "domain": "usageLimits",
     "reason": "accessNotConfigured",
     "message": "Access Not Configured"
    }
   ],
   "code": 403,
   "message": "Access Not Configured"
  }
 }

使用简单API访问:API密钥

Using Simple API Access: API key

Exceptioncom.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
{
  "code" : 401,
  "errors" : [ {
    "domain" : "global",
    "location" : "Authorization",
    "locationType" : "header",
    "message" : "Invalid Credentials",
    "reason" : "authError"
  } ],
  "message" : "Invalid Credentials"
}

下面是项目源代码:

http://www.sendspace.com/file/an6xxy

什么是错的,或许真的有证书的指纹(SHA1)?

What is wrong, maybe something with Certificate fingerprint (SHA1)?

这是谷歌试图日历Android的样本。

Trying calendar-android-sample from google.

看起来像我有在谷歌API控制台寄存器应用问题。

Looks like i have problems with register app in google api console.

如果把客户端ID,我在样品得到什么?

Where to put client id which i get in sample?

在在在Android中,越来越调试模式下运行样品访问未配置。

After run sample in debug mode in android, getting "access not configured".

推荐答案

在Android上,最好的做法是使用谷歌进行的OAuth 2.0播放服务。由于库版本1.12.0-β,你应该使用<一个href=\"http://javadoc.google-api-java-client.google$c$c.com/hg/1.12.0-beta/com/google/api/client/googleapis/extensions/android/gms/auth/GoogleAccountCredential.html\"相对=nofollow> GoogleAccountCredential 做到这一点。

On Android, the best practice is to use Google Play services for OAuth 2.0. As of version 1.12.0-beta of the library, you should use GoogleAccountCredential to accomplish this.

请使用我写<一个例子href=\"http://samples.google-api-java-client.google$c$c.com/hg/calendar-android-sample/instructions.html\"相对=nofollow>日历Android的样本作为Android上的OAuth 2.0的最佳实践指南。请仔细阅读说明书。请注意,您必须先注册在谷歌API的控制台你申请这个工作。从样本code片断:

Please use the example I wrote calendar-android-sample as a guide for the best practice for OAuth 2.0 on Android. Please read the instructions carefully. Note that you must first register your application in the Google APIs Console for this to work. Code snippet from the sample:

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  ...
  // Google Accounts
  credential = GoogleAccountCredential.usingOAuth2(this, CalendarScopes.CALENDAR);
  SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
  credential.setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null));
  // Calendar client
  client = new com.google.api.services.calendar.Calendar.Builder(
      transport, jsonFactory, credential).setApplicationName("Google-CalendarAndroidSample/1.0")
      .build();
}

这篇关于该方法setJsonHtt prequestInitializer是未定义的类型Drive.Builder的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆