我正在使用GoogleAccountManager使用oauth2进行授权并获取AUTHTOKEN,现在不做下一步操作.
要创建Drive对象,我需要GoogleCredential才能获得它们?
Drive service = new Drive(TRANSPORT, JSON_FACTORY, credential);
也许我应该对Connect to the Online Service教程做类似的事情?
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);
然后这个网址来自“https://www.googleapis.com/tasks/v1/users/@me/lists?key=”
解决方法:
此答案现在已过时,因为已弃用AccountManager身份验证以支持Play服务.
新答案
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();
原始答案
我用过这个. AuthToken是您从AccountManager收到的令牌. ApiKey是你从Google Apis Console获得的关键.
到目前为止似乎工作.文档很差.希望随着它的成熟,它会有所改善.它似乎是为已经知道他们在访问G Api时所做的事情的人写的.完整的样本可以节省很多时间.
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();
}