【问题标题】:Oauth 2.0 with grant_type=client_credentials?Oauth 2.0 与 grant_type=client_credentials?
【发布时间】:2021-05-10 14:09:51
【问题描述】:

我正在寻找 Java 中的 OAuth 2.0 客户端,它支持通过 grant_type=client_credentials 进行机器对机器通信。 有什么建议吗?

编辑:我在这里询问,因为到目前为止 2 天的研究表明,OAuth 2.0 有很多库,但似乎没有一个库支持所需的操作模式。

【问题讨论】:

    标签: java oauth-2.0


    【解决方案1】:

    经过多次头疼,我终于发现 ScribeJava 支持所需的操作模式——尽管它有些隐藏。

    这是我采用的版本:

        public static void main(String... args) throws IOException, InterruptedException, ExecutionException {
            // Replace these with your client id and secret
            final String clientId = "your client id";
            final String clientSecret = "your client secret";
            final OAuth20Service service = new ServiceBuilder(clientId)
                    .apiSecret(clientSecret)
                    .defaultScope("any") // replace with desired scope
                    .build(new DefaultApi20() {
                        @Override
                        public String getAccessTokenEndpoint() {
                            return "http://127.0.0.1:8082/token";
                        }
    
                        @Override
                        protected String getAuthorizationBaseUrl() {
                            throw new UnsupportedOperationException(
                                    "This API doesn't support a Base URL.");
                        }
                    });
            
            System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
            System.out.println();
            final OAuth2AccessToken accessToken = service.getAccessTokenClientCredentialsGrant();
    
            System.out.println("Got the Access Token!");
            System.out.println(accessToken.getRawResponse());
            System.out.println();
    
            System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)");
        }
    

    原始示例可以在https://github.com/scribejava/scribejava/blob/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/VkontakteClientCredentialsGrantExample.java找到

    【讨论】:

      猜你喜欢
      • 2018-10-04
      • 2018-11-17
      • 1970-01-01
      • 2019-08-28
      • 2017-11-14
      • 2015-10-07
      • 2020-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多