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

Dmy*_*lyk 3 android google-drive-api

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

我正在使用GoogleAccountManager使用oauth2进行授权并获取AUTHTOKEN,现在不做下一步操作.

要创建Drive对象,我需要GoogleCredential才能获得它们?

Drive service = new Drive(TRANSPORT, JSON_FACTORY, credential);
Run Code Online (Sandbox Code Playgroud)

也许我应该做类似的事情连接到在线服务教程?

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);
Run Code Online (Sandbox Code Playgroud)

然后这个网址来自"https://www.googleapis.com/tasks/v1/users/@me/lists?key="

请给我建议或示例代码如何在谷歌驱动器上加载文件.谢谢.

Kuf*_*ffs 5

此答案现在已过时,因为已弃用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();
Run Code Online (Sandbox Code Playgroud)

原始答案

我用过这个. AuthToken是您从AccountManager收到的令牌.ApiKey是您从Google Apis控制台获得的密钥.

到目前为止似乎工作.文档很差.希望随着它的成熟,它会有所改善.它似乎是为已经知道他们在访问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();
}
Run Code Online (Sandbox Code Playgroud)

  • 您设置了哪种类型的应用程序来获取客户ID以从Google的API控制台获取ApiKey?(IE,你选择"Android"还是"其他"?) (2认同)