Google ドライブのファイルを更新する Java プロジェクトを実行する必要があります。私はこのチュートリアルに従いました。
テストで行ったように、クライアントIDとクライアントシークレットではなく、ユーザー名/パスワードでアクセスしたい:
package test_api_google;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.http.FileContent;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class DriveCommandLine
{
private static String CLIENT_ID = "*****************************";
private static String CLIENT_SECRET = "----------------------";
private static String REDIRECT_URI = "oishdpi:g57s5g:464ty6";
/* ________________________________________________ */
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET,
Arrays.asList(DriveScopes.DRIVE)).setAccessType("online")
.setApprovalPrompt("auto").build();
String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build();
System.out.println("Please open the following URL in your browser then type the authorization code:");
System.out.println(" " + url);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String code = br.readLine();
GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();
GoogleCredential credential = new GoogleCredential().setFromTokenResponse(response);
// Create a new authorized API client
Drive service = new Drive.Builder(httpTransport, jsonFactory,
credential).build();
// Insert a file
File body = new File();
body.setTitle("CHOUETTE");
body.setDescription("A test document");
body.setMimeType("plain/text");
body.setEditable(true);
java.io.File fileContent = new java.io.File("src/data/document.txt");
FileContent mediaContent = new FileContent("text/plain", fileContent);
File file = service.files().insert(body, mediaContent).execute();
System.out.println("File ID: " + file.getId());
// Test lecture de fichiers
File lectureFichier = new File();
lectureFichier = service.files().get(file.getId()).execute();
System.out.println(lectureFichier.getTitle());
FileList listeFichiers = service.files().list().execute();
int nbFichiers = listeFichiers.getItems().size();
for (int i = 0; i < nbFichiers; i++)
{
File fichier = listeFichiers.getItems().get(i);
if (fichier.getTitle().equals("My document"))
{
System.out.println("FICHIER TROUVE");
break ;
}
}
}
}
Google ドライブ アカウントで新しいファイルを作成し、それを見つけることができます。ユーザー名/パスワードを使用して同じことを行うにはどうすればよいですか。
接続情報を使用してクライアント ID とクライアント シークレットを作成する方法はありますか? 私もファイルを更新する方法を見つける必要がありますか?