Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add the example of calling github api with scrible-java.

  • Loading branch information...
commit a74bec794553f0e42f3fcb4eeeff5dfa5723e217 1 parent cf9c4be
@yosaku01 authored
View
47 src/main/java/org/scribe/builder/api/GitHubApi.java
@@ -0,0 +1,47 @@
+package org.scribe.builder.api;
+
+
+import org.scribe.builder.api.DefaultApi20;
+import org.scribe.extractors.GitHubTokenExtractor;
+import org.scribe.model.OAuthConfig;
+import org.scribe.model.Verb;
+import org.scribe.utils.OAuthEncoder;
+
+public class GitHubApi extends DefaultApi20 {
+
+ private static final String AUTHORIZE_URL = "https://github.com/login/oauth/authorize?client_id=%s";
+ private static final String SCOPED_AUTHORIZE_URL = AUTHORIZE_URL + "&scope=%s";
+
+
+ @Override
+ public String getAccessTokenEndpoint()
+ {
+ return "https://github.com/login/oauth/access_token";
+ }
+
+ @Override
+ public String getAuthorizationUrl(OAuthConfig config)
+ {
+ // Append scope if present
+ if (config.hasScope())
+ {
+ return String.format(SCOPED_AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()), OAuthEncoder.encode(config.getScope()));
+ }
+ else
+ {
+ return String.format(AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()));
+ }
+ }
+
+ @Override
+ public Verb getAccessTokenVerb()
+ {
+ return Verb.POST;
+ }
+
+ @Override
+ public GitHubTokenExtractor getAccessTokenExtractor()
+ {
+ return new GitHubTokenExtractor();
+ }
+}
View
27 src/main/java/org/scribe/extractors/GitHubTokenExtractor.java
@@ -0,0 +1,27 @@
+package org.scribe.extractors;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.scribe.exceptions.OAuthException;
+import org.scribe.model.Token;
+import org.scribe.utils.Preconditions;
+
+public class GitHubTokenExtractor implements AccessTokenExtractor
+{
+
+ private Pattern accessTokenPattern = Pattern.compile("access_token=([^&]+)");
+ public Token extract(String response)
+ {
+ Preconditions.checkEmptyString(response, "Cannot extract a token from a null or empty String");
+ Matcher matcher = accessTokenPattern.matcher(response);
+ if(matcher.find())
+ {
+ return new Token(matcher.group(1), "", response);
+ }
+ else
+ {
+ throw new OAuthException("Cannot extract an acces token. Response was: " + response);
+ }
+ }
+}
View
75 src/test/java/org/scribe/examples/GitHubExample.java
@@ -0,0 +1,75 @@
+package org.scribe.examples;
+
+import java.util.Scanner;
+
+import org.scribe.builder.ServiceBuilder;
+import org.scribe.builder.api.GitHubApi;
+
+
+import org.scribe.model.OAuthRequest;
+import org.scribe.model.Response;
+import org.scribe.model.Token;
+import org.scribe.model.Verb;
+import org.scribe.model.Verifier;
+import org.scribe.oauth.OAuthService;
+
+
+/**
+ * Hello world!
+ *
+ */
+public class GitHubExample
+{
+ private static final Token EMPTY_TOKEN = null;
+ public static void main( String[] args )
+ {
+ try
+ {
+ // Replace these with your own api key and secret
+ String apiKey = "You GitHub client id";
+ String apiSecret = "You Git client secret";
+ OAuthService service = new ServiceBuilder().provider(GitHubApi.class)
+ .apiKey(apiKey)
+ .apiSecret(apiSecret)
+ .callback("You github's redirect uri")
+ .build();
+ Scanner in = new Scanner(System.in);
+
+ // Obtain the Authorization URL
+ System.out.println("Fetching the Authorization URL...");
+ String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);
+ System.out.println("Got the Authorization URL!");
+ System.out.println("Now go and authorize Scribe here:");
+ System.out.println(authorizationUrl);
+ System.out.println("And paste the authorization code here");
+ System.out.print(">>");
+ String nextLine = "";
+
+ nextLine = in.nextLine();
+ Verifier verifier = new Verifier(nextLine);
+ System.out.println();
+
+ // Trade the Request Token and Verifier for the Access Token
+ System.out.println("Trading the Request Token for an Access Token...");
+ Token accessToken = service.getAccessToken(EMPTY_TOKEN, verifier);
+ System.out.println("Got the Access Token!");
+ System.out.println("(if your curious it looks like this: " + accessToken + " )");
+ System.out.println();
+
+ OAuthRequest request = new OAuthRequest(Verb.GET, "https://api.github.com/users/YourGitHubAccount");
+ service.signRequest(accessToken, request); // the access token from step 4
+ Response response = request.send();
+ System.out.println("Got it! Lets see what we found...");
+ System.out.println();
+ System.out.println(response.getCode());
+ System.out.println(response.getBody());
+
+ System.out.println();
+ System.out.println("Thats it man! Go and build something awesome with Scribe! :)");
+ }
+ catch(Exception ex)
+ {
+ ex.printStackTrace();
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.