【发布时间】:2017-11-25 18:27:36
【问题描述】:
我正在尝试使用 Google 驱动器的 API 来开发独立的 Java 应用程序(仅使用 Java SE)。为此,我需要使用 Oauth2.0 获取访问令牌。为了获取访问令牌,我编写了以下代码:
package Drive;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import static Drive.Constants.*;
public class Test {
public static void main(String[] args) throws IOException {
String url=OAuth2URL+"redirect_uri=https://localhost/"+"&"+"client_id="+CLIENTID+"&"+"scope="+SCOPE+"&"+
"access_type=offline&"+"response_type=code";
System.out.println("URL is :"+url);
HttpClient httpClient= new DefaultHttpClient();
HttpGet httpGet=new HttpGet(url);
HttpResponse response=httpClient.execute(httpGet);
if(response.getEntity().getContent()!=null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String str = null;
while ((str = reader.readLine()) != null) {
System.out.println(str);
}
}
}
}
我已将我的凭据和 oauth2.0 url 放在常量字符串中。我的问题是如何 我可以在我的应用程序中获取此 API 调用的响应吗?我需要分叉吗 在特定端口上监听进程以获取响应?
【问题讨论】:
标签: java google-drive-api google-oauth google-drive-android-api apache-commons-httpclient