【问题标题】:Getting Access token using Oauth2 with standalone java application使用带有独立 Java 应用程序的 Oauth2 获取访问令牌
【发布时间】: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


    【解决方案1】:

    redirect_uri 应该在 google 控制台上注册(在您获得 client_id 的地方),并且您应该将此 URL 公开为真正的 http/s 端点。

    有关详细信息,请参阅此处的第 2 步:https://developers.google.com/identity/protocols/OAuth2InstalledApp

    【讨论】:

    猜你喜欢
    • 2013-12-04
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多