我正在使用OAuth2.0进行谷歌驱动器身份验证。它的工作正常。只要用户单击身份验证,它就会在浏览器中打开一个新选项卡以获取用户权限。我无法在新标签中获取它打开的网址。我想远程访问我的Web应用程序。它不起作用,因为验证窗口在运行应用程序的默认浏览器中打开。有没有办法获得完整的Url?
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
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.googleapis.javanet.GoogleNetHttpTransport;
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.client.util.store.FileDataStoreFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.Drive.Files;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.FileList;
public class GoolgeDriveUpload3 {
private static String CLIENT_ID = "xxxxxxxxxx";
private static String CLIENT_SECRET = "yyyyyyyyyy";
static HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
static JsonFactory jsonFactory = new JacksonFactory();
private static FileDataStoreFactory DATA_STORE_FACTORY;
private static final java.io.File DATA_STORE_DIR = new java.io.File(System.getProperty("user.home"),
".credentials/drive-java-quickstart");
static {
try {
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
} catch (Throwable t) {
t.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) throws IOException {
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, jsonFactory,
CLIENT_ID, CLIENT_SECRET, Arrays.asList(DriveScopes.DRIVE_FILE)).setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("offline").setApprovalPrompt("auto").build();
Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalCallbackServer()).authorize("user");
Drive service = new Drive.Builder(HTTP_TRANSPORT, jsonFactory, credential).build();
List<File> result = new ArrayList<File>();
Files.List request = null;
request = service.files().list();
FileList files = request.setQ("'root' in parents and trashed=false ").execute();
result.addAll(files.getItems());
request.setPageToken(files.getNextPageToken());
for (File f : result) {
System.out.println("Files are: " + f.getTitle() + " " + f.getId() + " " + f.getAlternateLink());
}
}
}
import com.google.api.client.extensions.java6.auth.oauth2.VerificationCodeReceiver;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
public class LocalCallbackServer implements VerificationCodeReceiver {
volatile String code;
private final int LOCAL_SERVER_PORT = 9058;
@Override
public synchronized String waitForCode() {
try {
this.wait();
} catch (Exception ex) {
}
System.out.println("returning code is -> " + code);
return code;
}
@Override
public String getRedirectUri() {
new Thread(new MyThread()).start();
return "http://127.0.0.1:" + LOCAL_SERVER_PORT;
}
@Override
public void stop() {
}
class MyThread implements Runnable {
@Override
public void run() {
try {
// return GoogleOAuthConstants.OOB_REDIRECT_URI;
ServerSocket ss = new ServerSocket(LOCAL_SERVER_PORT);
System.out.println("server is ready...");
Socket socket = ss.accept();
System.out.println("new request....");
InputStream is = socket.getInputStream();
StringWriter writer = new StringWriter();
String firstLine = null;
InputStreamReader isr = new InputStreamReader(is);
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(isr);
String read = br.readLine();
firstLine = read;
OutputStream os = socket.getOutputStream();
PrintWriter out = new PrintWriter(os, true);
StringTokenizer st = new StringTokenizer(firstLine, " ");
st.nextToken();
String codeLine = st.nextToken();
st = new StringTokenizer(codeLine, "=");
st.nextToken();
code = st.nextToken();
out.write("RETURNED CODE IS " + code + "");
out.flush();
// is.close();
socket.close();
System.out.println("Extracted coded is " + code);
synchronized (LocalCallbackServer.this) {
LocalCallbackServer.this.notify();
}
System.out.println("return is " + sb.toString());
} catch (IOException ex) {
Logger.getLogger(LocalCallbackServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}