Monday 14, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Thursday, 14 July 2016

GitHub OAuth 2.0 Example using Scribe Java

In this example we are going to see about how to use GitHub OAuth 2.0 using Scribe Java

GitHub OAuth 2.0 Example using Scribe

GitHubExample.java

01.import java.util.Random;
02.import java.util.Scanner;
03.import com.github.scribejava.apis.GitHubApi;
04.import com.github.scribejava.core.builder.ServiceBuilder;
05.import com.github.scribejava.core.model.OAuth2AccessToken;
06.import com.github.scribejava.core.model.OAuthRequest;
07.import com.github.scribejava.core.model.Response;
08.import com.github.scribejava.core.model.Verb;
09.import com.github.scribejava.core.oauth.OAuth20Service;
10.import java.io.IOException;
11. 
12.public abstract class GitHubExample {
13. 
14.    private static final String NETWORK_NAME = "GitHub";
15.    private static final String PROTECTED_RESOURCE_URL = "http://ift.tt/N8YLMI";
16. 
17.    public static void main(String... args) throws IOException {
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 String secretState = "secret" + new Random().nextInt(999_999);
22.        final OAuth20Service service = new ServiceBuilder()
23.                .apiKey(clientId)
24.                .apiSecret(clientSecret)
25.                .state(secretState)
26.                .callback("http://ift.tt/29ygMme")
27.                .build(GitHubApi.instance());
28.        final Scanner in = new Scanner(System.in, "UTF-8");
29. 
30.        System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
31.        System.out.println();
32. 
33.        // Obtain the Authorization URL
34.        System.out.println("Fetching the Authorization URL...");
35.        final String authorizationUrl = service.getAuthorizationUrl();
36.        System.out.println("Got the Authorization URL!");
37.        System.out.println("Now go and authorize ScribeJava here:");
38.        System.out.println(authorizationUrl);
39.        System.out.println("And paste the authorization code here");
40.        System.out.print(">>");
41.        final String code = in.nextLine();
42.        System.out.println();
43. 
44.        System.out.println("And paste the state from server here. We have set 'secretState'='" + secretState + "'.");
45.        System.out.print(">>");
46.        final String value = in.nextLine();
47.        if (secretState.equals(value)) {
48.            System.out.println("State value does match!");
49.        } else {
50.            System.out.println("Ooops, state value does not match!");
51.            System.out.println("Expected = " + secretState);
52.            System.out.println("Got      = " + value);
53.            System.out.println();
54.        }
55. 
56.        // Trade the Request Token and Verfier for the Access Token
57.        System.out.println("Trading the Request Token for an Access Token...");
58.        final OAuth2AccessToken accessToken = service.getAccessToken(code);
59.        System.out.println("Got the Access Token!");
60.        System.out.println("(if your curious it looks like this: " + accessToken
61.                + ", 'rawResponse'='" + accessToken.getRawResponse() + "')");
62.        System.out.println();
63. 
64.        // Now let's go and ask for a protected resource!
65.        System.out.println("Now we're going to access a protected resource...");
66.        final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL, service);
67.        service.signRequest(accessToken, request);
68.        final Response response = request.send();
69.        System.out.println("Got it! Lets see what we found...");
70.        System.out.println();
71.        System.out.println(response.getCode());
72.        System.out.println(response.getBody());
73. 
74.        System.out.println();
75.        System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)");
76.    }
77.}

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • AWeber OAuth 1.0a Example using Scribe JavaIn this example we are going to see about how to use AWeber OAuth 1.0a using Scribe Java AWeber OAuth 1.0a Example using Scribe AWeberExample.java import java.util.Scanner; import com.github.scribejava.core.builder.ServiceBu… Read More
  • Flickr OAuth 1.0a Example using Scribe JavaIn this example we are going to see about how to use Flickr OAuth 1.0a using Scribe Java Flickr OAuth 1.0a Example using Scribe FlickrExample.java import java.util.Scanner; import com.github.scribejava.core.builder.ServiceBu… Read More
  • LinkedIn OAuth 1.0a with Scope Example using Scribe JavaIn this example we are going to see about how to use LinkedIn OAuth 1.0a using Scribe Java LinkedIn OAuth 1.0a Example with Scope LinkedInExampleWithScopes.java import java.util.Scanner; import com.github.scribejava.core.bui… Read More
  • Yahoo OAuth 1.0a Example using Scribe JavaIn this example we are going to see about how to use Yahoo OAuth 1.0a using Scribe Java Yahoo OAuth 1.0a Example with Scope YahooExample.java import java.util.Scanner; import com.github.scribejava.core.builder.ServiceBuilder… Read More
  • Twiter OAuth 1.0a Example using Scribe JavaIn this example we are going to see about how to use Twitter OAuth 1.0a using Scribe Java Twitter OAuth 1.0a Example with Scope TwitterExample.java import java.util.Scanner; import com.github.scribejava.core.builder.ServiceB… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: GitHub OAuth 2.0 Example using Scribe Java Rating: 5 Reviewed By: eHowToNow