Reputation: 19788
What I need to do is to load file on google drive.
I am using GoogleAccountManager to authorize with oauth2 and get AUTHTOKEN and don't now what to do next.
To create Drive object I need GoogleCredential where I can get them?
Drive service = new Drive(TRANSPORT, JSON_FACTORY, credential);
Maybe I Should do similar thing to Connect to the Online Service tutorial?
URL url = new URL("https://www.googleapis.com/tasks/v1/users/@me/lists?key=" + your_api_key);
URLConnection conn = (HttpURLConnection) url.openConnection();
conn.addRequestProperty("client_id", your client id);
conn.addRequestProperty("client_secret", your client secret);
conn.setRequestProperty("Authorization", "OAuth " + token);
Then where this url comes from "https://www.googleapis.com/tasks/v1/users/@me/lists?key="
Please give me advice or sample code how to load file on google drive. Thanks.
Upvotes: 3
Views: 2128
Reputation: 35661
This answer is now out of date as AccountManager authentyication has been deprecated in favour of Play Services.
New Answer
private Drive service;
private GoogleAccountCredential credential;
credential = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE);
credential.setSelectedAccountName(accountName);
service = new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential).build();
Original answer
I used this. AuthToken is the token you received from AccountManager. ApiKey is the key you get from your Google Apis Console.
Seems to work so far. The documentation is very poor. Hopefully it will improve as it matures. It seems to be written for people who already know what they are doing regarding accessing G Api's. A complete sample would save so much time.
static Drive buildService(final String AuthToken, final String ApiKey) {
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
Drive.Builder b = new Drive.Builder(httpTransport, jsonFactory, null);
b.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
@Override
public void initialize(JsonHttpRequest request) throws IOException {
DriveRequest driveRequest = (DriveRequest) request;
driveRequest.setPrettyPrint(true);
driveRequest.setKey(ApiKey);
driveRequest.setOauthToken(AuthToken);
}
});
return b.build();
}
Upvotes: 5