简体   繁体   English

使用Oauth2和独立的Java应用程序获取访问令牌

[英]Getting Access token using Oauth2 with standalone java application

I am trying to use Google drive's APIs to develop a standalone java application(using only java SE). 我正在尝试使用Google驱动器的API开发独立的Java应用程序(仅使用Java SE)。 For that I need to get access token using Oauth2.0.For getting the access token I have written following code: 为此,我需要使用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);
                }
            }

        }
    }

I have put my credentials and oauth2.0 url in constant strings.My question is how can I get the response of this API call in to my application? 我已经将我的凭据和oauth2.0 url放在了常量字符串中。我的问题是如何获取此API调用的响应到我的应用程序中? Do I need to fork a listening process on specific port to get the response? 我是否需要在特定端口上派一个侦听进程以获取响应?

The redirect_uri should be registered on the google console (in the place you get your client_id ), and you should expose this url as a real http/s endpoint. redirect_uri应该在Google控制台上注册(在获取client_id ),并且您应该将此网址公开为真实的http / s端点。

For more info see Step 2 Here: https://developers.google.com/identity/protocols/OAuth2InstalledApp 有关更多信息,请参见此处的步骤2: https : //developers.google.com/identity/protocols/OAuth2InstalledApp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 java 从 Oauth2 rest api 获取访问令牌 - Get access token from Oauth2 rest api using java 获取OAUTH2令牌 - Getting the OAUTH2 Token 如何使用 Spring Security 5 在 Spring Boot 应用程序(不是 Web 应用程序)中获取 oauth2 访问令牌 - How to get oauth2 access token in a spring boot application (not a web application) using spring security 5 Spring Boot 2.0.3 Oauth2安全性:即使在标头中使用访问令牌也会出现401错误 - Spring Boot 2.0.3 Oauth2 Security: Getting 401 error even when using access token in header WSO2 IS OAuth2资源所有者密码-获取访问令牌 - WSO2 IS OAuth2 Resource owner password - getting access token 带有Spring Boot REST应用程序的OAuth2 - 无法使用令牌访问资源 - OAuth2 with Spring Boot REST application - cannot access resource with token 在OAuth2中获取访问令牌 - Obtaining an access token in OAuth2 在GAE Java应用程序中使用OAuth2身份验证通过IMAP访问用户GMail帐户 - Access user GMail account via IMAP using OAuth2 authentication in GAE Java application 使用放心的 Java 客户端获取 oauth2 令牌 - Get oauth2 token using rest assured Java client 从 Java 批处理应用程序访问受 OAuth2 保护的 api - Access OAuth2 Protected api from the Java Batch Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM