简体   繁体   中英

Oauth 2.0 with grant_type=client_credentials?

I am looking for a OAuth 2.0 client in Java which supports machine to machine communication via grant_type=client_credentials . Any recommendations?

Edit : I am asking here, since 2 days of research indicate so far, that there are many libs for OAuth 2.0, but none seems to support the required mode of operation.

After many headaches, I finally found that ScribeJava supports the needed mode of operations – although it's somewhat hidden.

Here is my adopted version:

    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! :)");
    }

The original example can be found at https://github.com/scribejava/scribejava/blob/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/VkontakteClientCredentialsGrantExample.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM