Drive API'sini biraz öğrenmek için DriveCommandLine uygulamasıyla uğraşıyorum. Masaüstü uygulamamın Google Drive ile kimlik doğrulaması yapmanın kullanıcı tarafından tarayıcıdan bir kimlik kodu kopyalama/yapıştırma ihtiyacı olmadan mümkün olup olmadığını merak ediyorum. Ancak, yalnızca bir kodun tarayıcıdan uygulamaya geri döndürülmesi yeterlidir? Bunu Dropbox API'sı ve Google Dokümanlar Listesi API'sı ile gerçekleştirebiliyorum, ancak bunu Google Drive API'sı ile nasıl çalıştıracağımızı anlayamıyorum.Kullanıcının kimlik doğrulama kodunu kopyalayıp yapıştırması gerekmeden Google Drive nasıl doğrulanır?
Teşekkürler.
Google Drive API - DriveCommandLine örnek uygulaması (biraz değiştirilmiş):
public class DriveCommandLine {
private static String CLIENT_ID = APPCONSTANTS.Google.CONSUMER_KEY;
private static String CLIENT_SECRET = APPCONSTANTS.Google.CONSUMER_SECRET;
private static String REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";
public static void main(String[] args) throws IOException, URISyntaxException {
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET, Arrays.asList(DriveScopes.DRIVE))
.setAccessType("offline")
.setApprovalPrompt("force").build();
String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build();
System.out.println("Enter authorization code:");
Desktop.getDesktop().browse(new URI(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();
}
Google Dokümanlar Listesi API:
public void authenticate(){
GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
oauthParameters.setOAuthConsumerKey(APPCONSTANTS.Google.CONSUMER_KEY);
OAuthSigner signer;
if (APPCONSTANTS.Google.USE_RSA_SIGNING) {
signer = new OAuthRsaSha1Signer(APPCONSTANTS.Google.CONSUMER_SECRET);
} else {
oauthParameters.setOAuthConsumerSecret(APPCONSTANTS.Google.CONSUMER_SECRET);
signer = new OAuthHmacSha1Signer();
}
GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(signer);
oauthParameters.setScope(APPCONSTANTS.Google.SCOPES);
oauthHelper.getUnauthorizedRequestToken(oauthParameters);
String requestUrl = oauthHelper.createUserAuthorizationUrl(oauthParameters);
Desktop desktop = Desktop.getDesktop();
URI url = new URI(requestUrl);
desktop.browse(url);
String token = oauthHelper.getAccessToken(oauthParameters);
}
Bu seçeneklerin her biri için "redirect_uri" değerlerini kullanıyor musunuz? Aynı akışları kullanıyor musunuz? –
Google Dokümanlar Listesi API'sı ile kimlik doğrulamasını yapmak için kodumu ekledim. – PseudoPsyche