Skip to content

Commit 68919e6

Browse files
committed
add support for client_credentials grant type (thanks to https://github.com/vivin)
1 parent 665092d commit 68919e6

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

changelog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* add PKCE (RFC 7636) support (Proof Key for Code Exchange by OAuth Public Clients) (thanks for suggesting to https://github.com/dieseldjango)
66
* switch to use HTTP Basic Authorization by default in requests with need of
77
(2.3. Client Authentication) https://tools.ietf.org/html/rfc6749#section-2.3 Can be overrided in API class
8+
* add support for client_credentials grant type (thanks to https://github.com/vivin)
89

910
[4.2.0]
1011
* DELETE in JdkClient permits, but not requires payload (thanks to https://github.com/miguelD73)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.github.scribejava.apis.examples;
2+
3+
import com.github.scribejava.core.builder.ServiceBuilder;
4+
import com.github.scribejava.apis.VkontakteApi;
5+
import com.github.scribejava.core.model.OAuth2AccessToken;
6+
import com.github.scribejava.core.oauth.OAuth20Service;
7+
import java.io.IOException;
8+
import java.util.concurrent.ExecutionException;
9+
10+
public final class VkontakteClientCredentialsGrantExample {
11+
12+
private static final String NETWORK_NAME = "Vkontakte.ru";
13+
14+
private VkontakteClientCredentialsGrantExample() {
15+
}
16+
17+
public static void main(String... args) throws IOException, InterruptedException, ExecutionException {
18+
// Replace these with your client id and secret
19+
final String clientId = "your client id";
20+
final String clientSecret = "your client secret";
21+
final OAuth20Service service = new ServiceBuilder(clientId)
22+
.apiSecret(clientSecret)
23+
.scope("wall,offline") // replace with desired scope
24+
.callback("http://your.site.com/callback")
25+
.build(VkontakteApi.instance());
26+
27+
System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
28+
System.out.println();
29+
final OAuth2AccessToken accessToken = service.getAccessTokenClientCredentialsGrant();
30+
31+
System.out.println("Got the Access Token!");
32+
System.out.println(accessToken);
33+
System.out.println();
34+
35+
System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)");
36+
}
37+
}

scribejava-core/src/main/java/com/github/scribejava/core/oauth/OAuth20Service.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,9 @@ public final Future<OAuth2AccessToken> getAccessTokenClientCredentialsGrant(
220220
protected OAuthRequest createAccessTokenClientCredentialsGrantRequest() {
221221
final OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
222222
final OAuthConfig config = getConfig();
223-
request.addParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
224-
final String apiSecret = config.getApiSecret();
225-
if (apiSecret != null) {
226-
request.addParameter(OAuthConstants.CLIENT_SECRET, apiSecret);
227-
}
223+
224+
api.getClientAuthenticationType().addClientAuthentication(request, config);
225+
228226
final String scope = config.getScope();
229227
if (scope != null) {
230228
request.addParameter(OAuthConstants.SCOPE, scope);

0 commit comments

Comments
 (0)