Skip to content

Commit 338406d

Browse files
tomekhotdogs-gromov
authored andcommitted
add Genius.com API authentication (OAuth2)
1 parent 53fbad0 commit 338406d

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

changelog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* simplify async/sync usages
33
* add optional "User-Agent" config option to use while making http calls
44
* refactor usage of grant_type [authorization_code|refresh_token|password|etc]
5+
* add Genius.com API authentication (OAuth2)
56

67
[2.5.3]
78
* fix - do not send two Content-Type header in async requests
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.github.scribejava.apis;
2+
3+
import com.github.scribejava.apis.service.GeniusOAuthServiceImpl;
4+
import com.github.scribejava.core.builder.api.DefaultApi20;
5+
import com.github.scribejava.core.model.OAuthConfig;
6+
import com.github.scribejava.core.oauth.OAuth20Service;
7+
import com.github.scribejava.core.utils.OAuthEncoder;
8+
9+
public class GeniusApi extends DefaultApi20 {
10+
11+
private static final String AUTHORIZE_URL = "https://api.genius.com/oauth/authorize"
12+
+ "?client_id=%s"
13+
+ "&redirect_uri=%s"
14+
+ "&scope=%s"
15+
+ "&state=%s"
16+
+ "&response_type=code";
17+
18+
private static final String TOKEN_ENDPOINT_URL = "https://api.genius.com/oauth/token";
19+
20+
protected GeniusApi() {
21+
}
22+
23+
private static class InstanceHolder {
24+
25+
private static final GeniusApi INSTANCE = new GeniusApi();
26+
}
27+
28+
public static GeniusApi instance() {
29+
return InstanceHolder.INSTANCE;
30+
}
31+
32+
@Override
33+
public String getAccessTokenEndpoint() {
34+
return TOKEN_ENDPOINT_URL;
35+
}
36+
37+
@Override
38+
public String getAuthorizationUrl(OAuthConfig config) {
39+
40+
// User must provide these 4 elements to the service builder
41+
return String.format(AUTHORIZE_URL,
42+
OAuthEncoder.encode(config.getApiKey()),
43+
OAuthEncoder.encode(config.getCallback()),
44+
OAuthEncoder.encode(config.getScope()),
45+
OAuthEncoder.encode(config.getState()));
46+
}
47+
48+
@Override
49+
public OAuth20Service createService(OAuthConfig config) {
50+
return new GeniusOAuthServiceImpl(this, config);
51+
}
52+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.github.scribejava.apis.service;
2+
3+
import com.github.scribejava.core.builder.api.DefaultApi20;
4+
import com.github.scribejava.core.model.AbstractRequest;
5+
import com.github.scribejava.core.model.OAuth2AccessToken;
6+
import com.github.scribejava.core.model.OAuthConfig;
7+
import com.github.scribejava.core.oauth.OAuth20Service;
8+
9+
public class GeniusOAuthServiceImpl extends OAuth20Service {
10+
11+
public GeniusOAuthServiceImpl(DefaultApi20 api, OAuthConfig config) {
12+
super(api, config);
13+
}
14+
15+
@Override
16+
public void signRequest(OAuth2AccessToken accessToken, AbstractRequest request) {
17+
request.addHeader("Authorization", "Bearer " + accessToken.getAccessToken());
18+
}
19+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.github.scribejava.apis.examples;
2+
3+
import java.util.Scanner;
4+
5+
import com.github.scribejava.apis.GeniusApi;
6+
import com.github.scribejava.core.builder.ServiceBuilder;
7+
import com.github.scribejava.core.model.OAuth2AccessToken;
8+
import com.github.scribejava.core.model.OAuthRequest;
9+
import com.github.scribejava.core.model.Response;
10+
import com.github.scribejava.core.model.Verb;
11+
import com.github.scribejava.core.oauth.OAuth20Service;
12+
13+
public abstract class GeniusExample {
14+
15+
private static final String NETWORK_NAME = "Genius";
16+
private static final String PROTECTED_RESOURCE_URL = "https://api.genius.com/songs/378195";
17+
18+
public static void main(String... args) {
19+
// Replace these with your client id and secret
20+
final String clientId = "your client id";
21+
final String clientSecret = "your client secret";
22+
final String secretState = "100";
23+
final OAuth20Service service = new ServiceBuilder()
24+
.apiKey(clientId)
25+
.apiSecret(clientSecret)
26+
.scope("me")
27+
.state(secretState)
28+
.callback("com.scribejavatest://callback")
29+
.userAgent("ScribeJava")
30+
.build(GeniusApi.instance());
31+
32+
System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
33+
System.out.println();
34+
35+
// Obtain the Authorization URL
36+
System.out.println("Fetching the Authorization URL...");
37+
final String authorizationUrl = service.getAuthorizationUrl();
38+
System.out.println("Got the Authorization URL!");
39+
System.out.println("Now go and authorize ScribeJava here:");
40+
System.out.println(authorizationUrl);
41+
System.out.println("And paste the authorization code here");
42+
System.out.print(">>");
43+
final String code;
44+
final String value;
45+
try (Scanner in = new Scanner(System.in, "UTF-8")) {
46+
code = in.nextLine();
47+
System.out.println();
48+
49+
System.out.println("And paste the state from server here. We have set 'secretState'='" + secretState
50+
+ "'.");
51+
System.out.print(">>");
52+
value = in.nextLine();
53+
}
54+
55+
if (secretState.equals(value)) {
56+
System.out.println("State value does match!");
57+
} else {
58+
System.out.println("Ooops, state value does not match!");
59+
System.out.println("Expected = " + secretState);
60+
System.out.println("Got = " + value);
61+
System.out.println();
62+
}
63+
64+
// Trade the Request Token and Verifier for the Access Token
65+
System.out.println("Trading the Request Token for an Access Token...");
66+
final OAuth2AccessToken accessToken = service.getAccessToken(code);
67+
System.out.println("Got the Access Token!");
68+
System.out.println("(View Access Token contents: " + accessToken
69+
+ ", 'rawResponse'='" + accessToken.getRawResponse() + "')");
70+
System.out.println();
71+
72+
// Now let's go and ask for a protected resource!
73+
System.out.println("Accessing a protected resource...");
74+
final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL, service);
75+
service.signRequest(accessToken, request);
76+
final Response response = request.send();
77+
System.out.println("Got it! Viewing contents...");
78+
System.out.println();
79+
System.out.println(response.getCode());
80+
System.out.println(response.getBody());
81+
82+
System.out.println();
83+
System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)");
84+
}
85+
}

0 commit comments

Comments
 (0)