Skip to content

Commit b8662a0

Browse files
committed
Merge commit 'a74bec7'; commit '165b3c5'; commit '2e18b4a'; commit '50451ed'; commit '5c331d9'; commit '745938e'; commit 'c51eb3e' into newapis
2 parents fa9810f + c51eb3e commit b8662a0

File tree

4 files changed

+138
-2
lines changed

4 files changed

+138
-2
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.scribe.builder.api;
2+
3+
import org.scribe.extractors.*;
4+
import org.scribe.model.*;
5+
import org.scribe.utils.*;
6+
7+
public class GitHubApi extends DefaultApi20
8+
{
9+
10+
private static final String AUTHORIZE_URL = "https://github.com/login/oauth/authorize?client_id=%s";
11+
private static final String SCOPED_AUTHORIZE_URL = AUTHORIZE_URL+ "&scope=%s";
12+
13+
@Override
14+
public String getAccessTokenEndpoint()
15+
{
16+
return "https://github.com/login/oauth/access_token";
17+
}
18+
19+
@Override
20+
public String getAuthorizationUrl(OAuthConfig config)
21+
{
22+
// Append scope if present
23+
if (config.hasScope())
24+
{
25+
return String.format(SCOPED_AUTHORIZE_URL, config.getApiKey(),
26+
OAuthEncoder.encode(config.getCallback()),
27+
OAuthEncoder.encode(config.getScope()));
28+
}
29+
else
30+
{
31+
return String.format(AUTHORIZE_URL, config.getApiKey(),
32+
OAuthEncoder.encode(config.getCallback()));
33+
}
34+
}
35+
36+
@Override
37+
public Verb getAccessTokenVerb()
38+
{
39+
return Verb.POST;
40+
}
41+
42+
@Override
43+
public GitHubTokenExtractor getAccessTokenExtractor()
44+
{
45+
return new GitHubTokenExtractor();
46+
}
47+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.scribe.extractors;
2+
3+
import java.util.regex.*;
4+
5+
import org.scribe.exceptions.*;
6+
import org.scribe.model.*;
7+
import org.scribe.utils.*;
8+
9+
public class GitHubTokenExtractor implements AccessTokenExtractor
10+
{
11+
12+
private Pattern accessTokenPattern = Pattern.compile("access_token=([^&]+)");
13+
14+
public Token extract(String response)
15+
{
16+
Preconditions.checkEmptyString(response,
17+
"Cannot extract a token from a null or empty String");
18+
Matcher matcher = accessTokenPattern.matcher(response);
19+
if (matcher.find())
20+
{
21+
return new Token(matcher.group(1), "", response);
22+
}
23+
else
24+
{
25+
throw new OAuthException(
26+
"Cannot extract an acces token. Response was: " + response);
27+
}
28+
}
29+
}

src/main/java/org/scribe/extractors/JsonTokenExtractor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
public class JsonTokenExtractor implements AccessTokenExtractor
1010
{
1111
private Pattern accessTokenPattern = Pattern.compile("\"access_token\":\\s*\"(\\S*?)\"");
12-
13-
@Override
12+
1413
public Token extract(String response)
1514
{
1615
Preconditions.checkEmptyString(response, "Cannot extract a token from a null or empty String");
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.scribe.examples;
2+
3+
import java.util.*;
4+
5+
import org.scribe.builder.*;
6+
import org.scribe.builder.api.*;
7+
import org.scribe.model.*;
8+
import org.scribe.oauth.*;
9+
10+
/**
11+
* GitHub API Example using Scrible-Java
12+
*
13+
*/
14+
public class GitHubExample
15+
{
16+
public static void main(String[] args)
17+
{
18+
// Replace these with your own api key and secret
19+
String apiKey = "You GitHub client id";
20+
String apiSecret = "You Git client secret";
21+
OAuthService service = new ServiceBuilder().provider(GitHubApi.class)
22+
.apiKey(apiKey)
23+
.apiSecret(apiSecret)
24+
.callback("You github's redirect uri")
25+
.build();
26+
Scanner in = new Scanner(System.in);
27+
28+
// Obtain the Authorization URL
29+
System.out.println("Fetching the Authorization URL...");
30+
String authorizationUrl = service.getAuthorizationUrl(null);
31+
System.out.println("Got the Authorization URL!");
32+
System.out.println("Now go and authorize Scribe here:");
33+
System.out.println(authorizationUrl);
34+
System.out.println("And paste the authorization code here");
35+
System.out.print(">>");
36+
String nextLine = "";
37+
38+
nextLine = in.nextLine();
39+
Verifier verifier = new Verifier(nextLine);
40+
System.out.println();
41+
42+
// Trade the Request Token and Verifier for the Access Token
43+
System.out.println("Trading the Request Token for an Access Token...");
44+
Token accessToken = service.getAccessToken(null, verifier);
45+
System.out.println("Got the Access Token!");
46+
System.out.println("(if your curious it looks like this: "+ accessToken + " )");
47+
System.out.println();
48+
49+
OAuthRequest request = new OAuthRequest(Verb.GET,
50+
"https://api.github.com/users/YourGitHubAccount");
51+
service.signRequest(accessToken, request); // the access token from step 4
52+
Response response = request.send();
53+
System.out.println("Got it! Lets see what we found...");
54+
System.out.println();
55+
System.out.println(response.getCode());
56+
System.out.println(response.getBody());
57+
58+
System.out.println();
59+
System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
60+
}
61+
}

0 commit comments

Comments
 (0)