Skip to content

Commit 3044949

Browse files
MichalFoksas-gromov
authored andcommitted
Stack Exchange authentication via OAuth 2.0 (stackoverflow.com, askubuntu.com, etc.).
1 parent 111fba3 commit 3044949

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

changelog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[SNAPSHOT]
2+
* Stack Exchange authentication via OAuth 2.0 (stackoverflow.com, askubuntu.com, etc.).
23
* Support response in gzip.
34

45
[2.2.2]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.github.scribejava.apis;
2+
3+
import com.github.scribejava.core.builder.api.DefaultApi20;
4+
import com.github.scribejava.core.model.OAuthConfig;
5+
import com.github.scribejava.core.model.OAuthConstants;
6+
import com.github.scribejava.core.model.Verb;
7+
import com.github.scribejava.core.utils.OAuthEncoder;
8+
import com.github.scribejava.core.utils.Preconditions;
9+
10+
/***
11+
* Stack Exchange authentication via OAuth 2.0 (stackoverflow.com,
12+
* askubuntu.com, etc.).
13+
*
14+
* @author Michal Foksa
15+
*
16+
*/
17+
public class StackExchangeApi extends DefaultApi20 {
18+
19+
private static final String AUTHORIZE_URL
20+
= "https://stackexchange.com/oauth?client_id=%s&redirect_uri=%s";
21+
22+
private StackExchangeApi() {
23+
}
24+
25+
private static class InstanceHolder {
26+
private static final StackExchangeApi INSTANCE = new StackExchangeApi();
27+
}
28+
29+
public static StackExchangeApi instance() {
30+
return InstanceHolder.INSTANCE;
31+
}
32+
33+
@Override
34+
public Verb getAccessTokenVerb() {
35+
return Verb.POST;
36+
}
37+
38+
@Override
39+
public String getAccessTokenEndpoint() {
40+
return "https://stackexchange.com/oauth/access_token";
41+
}
42+
43+
@Override
44+
public String getAuthorizationUrl(OAuthConfig config) {
45+
Preconditions.checkValidUrl(config.getCallback(),
46+
"Must provide a valid url as callback. StackExchange does not support OOB");
47+
final StringBuilder sb = new StringBuilder(String.format(AUTHORIZE_URL, config.getApiKey(),
48+
OAuthEncoder.encode(config.getCallback())));
49+
if (config.hasScope()) {
50+
sb.append('&').append(OAuthConstants.SCOPE).append('=').append(OAuthEncoder.encode(config.getScope()));
51+
}
52+
53+
final String state = config.getState();
54+
if (state != null) {
55+
sb.append('&').append(OAuthConstants.STATE).append('=').append(OAuthEncoder.encode(state));
56+
}
57+
return sb.toString();
58+
}
59+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.github.scribejava.apis.examples;
2+
3+
import java.util.Random;
4+
import java.util.Scanner;
5+
6+
import com.github.scribejava.apis.StackExchangeApi;
7+
import com.github.scribejava.core.builder.ServiceBuilder;
8+
import com.github.scribejava.core.model.OAuthRequest;
9+
import com.github.scribejava.core.model.Response;
10+
import com.github.scribejava.core.model.Token;
11+
import com.github.scribejava.core.model.Verb;
12+
import com.github.scribejava.core.model.Verifier;
13+
import com.github.scribejava.core.oauth.OAuth20Service;
14+
15+
public abstract class StackExchangeExample {
16+
17+
private static final String NETWORK_NAME = "Stack Exchange";
18+
private static final String PROTECTED_RESOURCE_URL = "https://api.stackexchange.com/2.2/me?site=stackoverflow&key=";
19+
20+
public static void main(String... args) {
21+
// Replace these with your client id and secret
22+
final String clientId = "your client id";
23+
final String clientSecret = "your client secret";
24+
final String key = "your client key";
25+
final String secretState = "secret" + new Random().nextInt(999_999);
26+
final OAuth20Service service = new ServiceBuilder()
27+
.apiKey(clientId)
28+
.apiSecret(clientSecret)
29+
.state(secretState)
30+
.callback("http://www.example.com/oauth_callback/")
31+
.build(StackExchangeApi.instance());
32+
final Scanner in = new Scanner(System.in, "UTF-8");
33+
34+
System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
35+
System.out.println();
36+
37+
// Obtain the Authorization URL
38+
System.out.println("Fetching the Authorization URL...");
39+
final String authorizationUrl = service.getAuthorizationUrl();
40+
System.out.println("Got the Authorization URL!");
41+
System.out.println("Now go and authorize ScribeJava here:");
42+
System.out.println(authorizationUrl);
43+
System.out.println("And paste the authorization code here");
44+
System.out.print(">>");
45+
final Verifier verifier = new Verifier(in.nextLine());
46+
System.out.println();
47+
48+
System.out.println("And paste the state from server here. We have set 'secretState'='" + secretState + "'.");
49+
System.out.print(">>");
50+
final String value = in.nextLine();
51+
if (secretState.equals(value)) {
52+
System.out.println("State value does match!");
53+
} else {
54+
System.out.println("Ooops, state value does not match!");
55+
System.out.println("Expected = " + secretState);
56+
System.out.println("Got = " + value);
57+
System.out.println();
58+
}
59+
60+
// Trade the Request Token and Verfier for the Access Token
61+
System.out.println("Trading the Request Token for an Access Token...");
62+
final Token accessToken = service.getAccessToken(verifier);
63+
System.out.println("Got the Access Token!");
64+
System.out.println("(if your curious it looks like this: " + accessToken + " )");
65+
System.out.println();
66+
67+
// Now let's go and ask for a protected resource!
68+
System.out.println("Now we're going to access a protected resource...");
69+
final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL + key, service);
70+
service.signRequest(accessToken, request);
71+
final Response response = request.send();
72+
System.out.println("Got it! Lets see what we found...");
73+
System.out.println();
74+
System.out.println(response.getCode());
75+
System.out.println(response.getBody());
76+
77+
System.out.println();
78+
System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)");
79+
80+
}
81+
}

0 commit comments

Comments
 (0)