`
m635674608
  • 浏览: 4930289 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

【Oauth认证】使用scribe实现OAUTH

    博客分类:
  • java
 
阅读更多

 Scribe这边指的是Oauth库, 项目地址: https://github.com/fernandezpablo85/scribe-java , 而不是Facebook那个(https://github.com/facebook/scribe)。

 

       支持Oauth1.0a\Oauth2.0,个人感觉很不错。

      

       用法其实很简单,看官网就可以了。这边附上里面的新浪微博请求实例:

 

 

  1. package com.yfz;  
  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. public class SinaWeiboExample  
  11. {  
  12.     private static final String NETWORK_NAME = "SinaWeibo";  
  13.     private static final String PROTECTED_RESOURCE_URL = "http://api.t.sina.com.cn/account/verify_credentials.json";  
  14.     private static final String API_KEY = "2178327133";  
  15.     private static final String API_SECRET = "67b0153c234c5423bcbba7078af6d127";  
  16.       
  17.     public static void main(String[] args)  
  18.     {  
  19.         // Replace these with your own api key and secret  
  20.         String apiKey = "your key";  
  21.         String apiSecret = "your secret";  
  22.         OAuthService service = new ServiceBuilder()  
  23.                                         .provider(SinaWeiboApi.class)  
  24.                                         .apiKey(apiKey)  
  25.                                         .apiSecret(apiSecret)  
  26.                                         .build();  
  27.         Scanner in = new Scanner(System.in);  
  28.   
  29.         System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");  
  30.         System.out.println();  
  31.   
  32.         // Grab a request token.  
  33.         System.out.println("Fetching request token.");  
  34.         Token requestToken = service.getRequestToken();  
  35.         System.out.println("Got it ... ");  
  36.         System.out.println(requestToken.getToken());  
  37.   
  38.         // Obtain the Authorization URL  
  39.         System.out.println("Fetching the Authorization URL...");  
  40.         String authorizationUrl = service.getAuthorizationUrl(requestToken);  
  41.         System.out.println("Got the Authorization URL!");  
  42.         System.out.println("Now go and authorize Scribe here:");  
  43.         System.out.println(authorizationUrl);  
  44.         System.out.println("And paste the authorization code here");  
  45.         System.out.print(">>");  
  46.         Verifier verifier = new Verifier(in.nextLine());  
  47.         System.out.println();  
  48.   
  49.         // Trade the Request Token and Verfier for the Access Token  
  50.         System.out.println("Trading the Request Token for an Access Token...");  
  51.         Token accessToken = service.getAccessToken(requestToken, verifier);  
  52.         System.out.println("Got the Access Token!");  
  53.         System.out.println("(if your curious it looks like this: "  
  54.                 + accessToken + " )");  
  55.         System.out.println();  
  56.   
  57.         // Now let's go and ask for a protected resource!  
  58.         System.out.println("Now we're going to access a protected resource...");  
  59.         OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);  
  60.         service.signRequest(accessToken, request);  
  61.         Response response = request.send();  
  62.         System.out.println("Got it! Lets see what we found...");  
  63.         System.out.println();  
  64.         System.out.println(response.getCode());  
  65.         System.out.println(response.getBody());  
  66.   
  67.         System.out.println();  
  68.         System.out.println("Thats it man! Go and build something awesome with Scribe! :)");  
  69.           
  70.     }  

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics