私はGoogleドライブを使用して、service
にファイルをアップロードして共有するアプリを開発しようとしています。しかし、問題は共有リンクがprivate
アクセス権を持っていることです。そのアクセスをanyone with link
に変更したいと思います。達成するために私はGoogle Drive REST API
を使用していますが、コードを実行すると何も起こらないようです。ここに私のコードは次のとおりです。Google APIに接続するバックグラウンドサービスのJavaクライアント
public void changePermissionSettings(String resourceId) throws GeneralSecurityException, IOException, URISyntaxException {
com.google.api.services.drive.Drive driveService = getDriveService();
JsonBatchCallback<Permission> callback = new JsonBatchCallback<com.google.api.services.drive.model.Permission>() {
@Override
public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) throws IOException {
Log.e("upload", "Permission Setting failed");
}
@Override
public void onSuccess(com.google.api.services.drive.model.Permission permission, HttpHeaders responseHeaders) throws IOException {
Log.e("upload", "Permission Setting success");
}
};
final BatchRequest batchRequest = driveService.batch();
com.google.api.services.drive.model.Permission contactPermission = new com.google.api.services.drive.model.Permission()
.setType("anyone")
.setRole("reader");
driveService.permissions().create(resourceId, contactPermission)
.setFields("id")
.queue(batchRequest, callback);
new Thread(new Runnable() {
@Override
public void run() {
try {
batchRequest.execute();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private com.google.api.services.drive.Drive getDriveService() throws GeneralSecurityException,
IOException, URISyntaxException {
Collection<String> elenco = new ArrayList<>();
elenco.add("https://www.googleapis.com/auth/drive");
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(this, elenco)
.setSelectedAccountName(getAccountName());
return new com.google.api.services.drive.Drive.Builder(
AndroidHttp.newCompatibleTransport(), new JacksonFactory(), credential).build();
}
私はここで2つの質問があります
はそれが正しい一緒にGoogleドライブのAndroid APIとGoogleドライブのREST APIの両方を使用することです??
ファイルの許可設定を変更するには
private
からanyone with link
にファイルを変更するにはどうすればいいですか? Documentationにはそのことが記載されていますAuthorization happens in response to receiving an error when sending a request.
service
でどのように達成できますか?