1

我试图使用Google驱动器的API来开发独立的Java应用程序(仅使用Java SE)。为此,我需要使用Oauth2.0.For让我写访问令牌下面的代码来获得访问令牌:使用独立Java应用程序使用Oauth2获取访问令牌

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的网址在不断strings.My问题是如何 我可以将此API调用的响应传递给我的应用程序吗?我是否需要在特定端口上分析 侦听进程以获取响应?

回答

3

redirect_uri应该在谷歌控制台上注册(在你得到你的地方client_id),你应该公开这个URL作为一个真正的http端点。

欲了解更多信息请参阅步骤2在这里:https://developers.google.com/identity/protocols/OAuth2InstalledApp

+0

嗨@Gal感谢您的帮助。但是我在oauth2凭证页面找不到redirect_uri。 –

+0

选择'Web Application'并查看'授权重定向URI'在这里:https://console.cloud.google.com/apis/credentials/oauthclient –

相关问题