로그인 한 사용자의 Google 드라이브에 파일을 저장해야하는 앱을 만들고있어 로그인 한 사용자가 드라이브에 데이터를 저장할 수 있습니다. 이제 코드에서 Google 드라이브 자격증 명 (clientId, clientSecret 및 refreshToken)을 생성하는 방법에 문제가 있습니다. 예제와 마찬가지로 Google 콘솔로 이동하여 요청합니다. 자바 코드를 사용하여 구현할 수 있습니다. 사실 나는 구글 드라이브 광고 데이터베이스 스토리지를 사용해야합니다. 아래 코드를 사용했지만 클라이언트 ID가 필요합니다.Google 드라이브 자격증 명
public class Main
{
public static void main(String[] args)
{
String clientId = "...";
String clientSecret = "...";
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport,
jsonFactory,
clientId,
clientSecret,
Arrays.asList(DriveScopes.DRIVE)
)
.setAccessType("online")
.setApprovalPrompt("auto").build();
String redirectUri = "urn:ietf:wg:oauth:2.0:oob";
String url =
flow
.newAuthorizationUrl()
.setRedirectUri(redirectUri)
.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(redirectUri)
.execute();
GoogleCredential credential =
new GoogleCredential()
.setFromTokenResponse(response);
Drive service =
new Drive.Builder(httpTransport, jsonFactory, credential)
.build();
...
}
}