设为首页 收藏本站
查看: 678|回复: 0

Windows Phone 7, Hammock, OAuth and Sina Weibo’s API

[复制链接]

尚未签到

发表于 2015-5-12 11:41:15 | 显示全部楼层 |阅读模式
Windows Phone 7, Hammock, OAuth and Sina Weibo’s API
  Link:http://www.binzywu.com/2011/02/windows-phone-7-hammock-oauth-and-sina-weibos-api/
  OK, I am developing a windows phone 7 app for Sina Weibo. I just simply don’t like the basic authorisation… And OAuth is much better since once you get the access token, you can use it all the time rather than always passing the username and password around. though there are already two Sina Weibo client apps (1 free and 1 is asking for $0.99) in marketplace, my friend Remy and I still want to develop a new app which will be free and open source.
  
  We restarted (or I can say started…) the development work from this Monday and things are in good progress. In the meantime, I would like to share what we’ve experienced during development and I hope this can help you if you are thinking to develop your own client on Windows Phone 7 or Silverlight for Sina Weibo. So this first entry is about getting OAuth access token for Sina Weibo via Hammock. Hammock is a great lib for you to consume and wrap RESTful services and it supports OAuth and XAuth.
  Step 1 register your app.
Sure, you need to apply/register an application onhttp://open.t.sina.com.cn/. You will then get your app key and secret (which is actually consumer key and secret).
  Step 2 authentication.
OAuth is quite simple and straightforward. In summary, you need to pass your consumer key and secret to get the request token, like this,

void OAuthTest()
{
    RestClient c = new RestClient()
    {
        Authority = "http://api.t.sina.com.cn/",
        HasElevatedPermissions = true,
        Credentials = new OAuthCredentials()
        {
            ConsumerKey = "your appkey",
            ConsumerSecret = "your appsecret",
            SignatureMethod = OAuthSignatureMethod.HmacSha1,
            ParameterHandling =
OAuthParameterHandling.HttpAuthorizationHeader,
            Version = "1.0"
        }
    };
    RestRequest r = new RestRequest()
    {
        Path = "oauth/request_token",
    };
    c.BeginRequest(r, new RestCallback(Callback));
}
void Callback(RestRequest request, RestResponse response,
object userState)
{
    Regex r =
new Regex("oauth_token=([^&.]*)&oauth_token_secret=([^&.]*)");
    var match = r.Match(response.Content);
    token = match.Groups[1].Value;
    tokensecret = match.Groups[2].Value;
}
  
once you get the request token and tokensecret, you can pass them along with consumer key for the authorization. Then Sina Weibo API will ask for username and password. But for client app like this, you may want user provide the info at your app and you take care the whole interaction in app. So you could generate a dynamic callback page. Beyond, the Sina Weibo API supports directly xml callback to return the xml which contains the oauth_verifier.

RestClient c = new RestClient()
{
    Authority = "http://api.t.sina.com.cn/",
};
RestRequest req = new RestRequest()
{
    Path = string.Format("oauth/authorize?{0}&oauth_callback=xml
&userId={1}&passwd={2}", response.Content, "user", "password")
};
c.BeginRequest(req, new RestCallback(Callback2));        
void Callback2(RestRequest request, RestResponse response,
object userState)
{
    Regex r = new Regex("<oauth_token>(.*?)</oauth_token>
<oauth_verifier>(.*?)</oauth_verifier>");
    var match = r.Match(response.Content);
}
  
then you are ready to ask for the access token,

RestClient c = new RestClient()
{
    Authority = "http://api.t.sina.com.cn/",
    HasElevatedPermissions = true,
    Credentials = new OAuthCredentials()
    {
        ConsumerKey = "your appkey",
        ConsumerSecret = "your appsecret",
        Token = "request token",
        TokenSecret = "request tokensecret",
        Verifier = "your oauth verifier from last step",
        SignatureMethod = OAuthSignatureMethod.HmacSha1,
        ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
        Type = OAuthType.AccessToken,
        Version = "1.0"
    }
};
RestRequest r2 = new RestRequest()
{
    Path = "oauth/access_token",
};
c.BeginRequest(r2, new RestCallback(Callback3));
public void Callback3(RestRequest request, RestResponse response,
object userState)
{
    // you will get format like this and that's your access
token: oauth_token=token&oauth_token_secret=secret&user_id=id
    var s = response.Content;
}
  
Step 3 make an update to your statuses!
As mentioned before once you’ve got the access token, the token can be used until the user revokes the authentication. The following things will be very simple. like if you want to post an update from your app, you just need some very familiar codes.

        private void Test()
        {
            RestClient c = new RestClient()
            {
                Authority = "http://api.t.sina.com.cn/",
                HasElevatedPermissions = true,
                Credentials = new OAuthCredentials()
                {
                    ConsumerKey = "your appkey",
                    ConsumerSecret = "your appsecret",
                    Token = "your access token",
                    TokenSecret = "your access tokensecret",
                    SignatureMethod = OAuthSignatureMethod.HmacSha1,
                    ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
                    Type = OAuthType.ProtectedResource,
                    Version = "1.0"
                }
            };
            RestRequest r2 = new RestRequest()
            {
                Path = "statuses/update.xml"
            };
            r2.AddParameter("status", "hello world");
            r2.Method = WebMethod.Post;
            c.BeginRequest(r2, new RestCallback(Callback));
        }
        void Callback(RestRequest request, RestResponse response,
object userState)
        {
            // check wheather post is successful
        }
  
Enjoy.
  
  references:
OAuth: http://oauth.net
Sina Weibo API: http://open.t.sina.com.cn/
Hammock: http://hammock.codeplex.com



18. February 2011 by binzywu
Categories: Mobile, Programming | Tags: .Net, API, c#, csharp, Hammock, OAuth,Sina, Weibo, windows phone 7, wp7, 客户端, 微博, 新浪 | Leave a comment

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-66223-1-1.html 上篇帖子: 解决Android 模拟器在Windows 7 x64下跳动的问题 下篇帖子: Windows Phone 7的控件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表