Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The method setJsonHttpRequestInitializer is undefined for the type Drive.Builder 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.

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

Where is the problem?

Source:

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();
} 

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

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

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"
  }
 }

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"
}

Here is project sources:

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

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

Trying calendar-android-sample from google.

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

Where to put client id which i get in sample?

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

like image 386
user1826780 Avatar asked Apr 26 '26 12:04

user1826780


1 Answers

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.

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();
}
like image 111
Yaniv Inbar Avatar answered Apr 29 '26 01:04

Yaniv Inbar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!