问题
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".
回答1:
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();
}
回答2:
You might be using an older version of the Java library.
Anyway, you can use the instance of the GoogleCredential
object as HttpRequestInitializer
, as in the following code:
private Drive getDriveService(String token) {
Credential credential = new GoogleCredential().setAccessToken(token);
Drive.Builder b = new Drive.Builder(
AndroidHttp.newCompatibleTransport(), new JacksonFactory(), credential)
.setHttpRequestInitializer(credential);
return b.build();
}
来源:https://stackoverflow.com/questions/13398607/the-method-setjsonhttprequestinitializer-is-undefined-for-the-type-drive-builder