• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

绝望生鱼片

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

【Oauth认证】使用scribe实现OAUTH

    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     }
71 }

有什么不明白的,可以留言。

 

附上新浪微博Oauth实例,比较简陋,例子中会先进行认证、然后获取用户资料,最后可以发布新微薄。 (注意看日志!)

例子下载地址:   http://download.csdn.net/detail/feng88724/3671033

 

转载自:http://blog.csdn.net/feng88724/article/details/6859502

posted on 2012-08-08 20:19  绝望生鱼片  阅读(483)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3