Я попытался загрузить изображение на Google Drive с помощью API привода, указанного на https://developers.google.com/drive/api/v3/quickstart/java.
public static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
public static final String TOKENS_DIRECTORY_PATH = "E:/tokens";
public static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE_APPDATA);
public static final String CREDENTIALS_FILE_PATH = "credentials.json";
public static Credential getCredentials(Context context, final NetHttpTransport HTTP_TRANSPORT) throws IOException {
InputStream in = context.getAssets().open(CREDENTIALS_FILE_PATH);
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build();
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}
А вот где я вызываю метод getCredentials.
private void uploadImageToDrive(Uri uri) throws IOException, GeneralSecurityException{
try {
GoogleDriveUpload gdu = new GoogleDriveUpload();
final NetHttpTransport HTTP_TRANSPORT = new com.google.api.client.http.javanet.NetHttpTransport();
Drive driveService = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, gdu.getCredentials(AccountInformationSignUpActivity.this, HTTP_TRANSPORT))
.setApplicationName(APPLICATION_NAME)
.build();
String folderId = "1B7EWlFJUGmQ8qHHgMFIzuGL0i-eXl3J_";
File fileMetadata = new File();
fileMetadata.setName("photo.jpg");
fileMetadata.setParents(Collections.singletonList(folderId));
java.io.File filePath = new java.io.File(uri.getPath());
FileContent mediaContent = new FileContent("image/jpeg", filePath);
File file = driveService.files().create(fileMetadata, mediaContent)
.setFields("id, parents")
.execute();
Log.d("File ID: ", file.getId());
} catch (FileNotFoundException e) {
e.getStackTrace();
}
}
Я помещаю файл credentials.json в main / assets / credentials.json
И он возвращает ошибку о невозможности создать каталог / токены.
Есть ли место, где я ошибаюсь?