android – 如何在谷歌驱动器上加载文件

我需要做的是在谷歌驱动器上加载文件.

我正在使用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();
}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...