`
luotuoass
  • 浏览: 640566 次
文章分类
社区版块
存档分类
最新评论

搜狐微博XAuth使用举例

 
阅读更多

搜狐微博XAuth使用举例

xAuth简介

使用xAuth认证方式,您仍然需要了解如何生成OAuth签名。

为了方便桌面应用和移动应用,特别是那些缺乏浏览器支持的应用,xAuth认证为这类应用提供了一种使用用户名和密码来获取OAuth的 Access Token的方式。 采用xAuth认证的桌面应用和移动应用可以跳过oauth/request_token(获取Request Token)以及oauth/authorize(授权Request Token)两步,只要提供了username和password以后,即可直接通过oauth/access_token接口得到Access Token。

使用xAuth

要使用xAuth认证,需要经过以下步骤:

1. 申请xAuth认证使用资格:现在Xauth权限已经对所有客户端类型应用直接开放。只要在申请应用时选择应用类型为“客户端”,都将直接开通Xauth权限。没有开通xauth权限的key将会返回403,forbidden。如果您已经获得xAuth使用权,请直接看第2步。

2. 获得xAuth的使用权后,为了能够生成OAuth签名,首先应该生成OAuth的BaseString。注意,生成BaseString时需要传入如下几个参数:

* x_auth_username:用户名

* x_auth_password:密码

* x_auth_mode:标识字段,这里必须是"client_auth"。

* oauth_consumer_key: 创建应用时生成的APP KEY。

* oauth_signature_method: 签名方法,建议使用“HMAC-SHA1”。

* oauth_timestamp:时间戳。生成Base String时的时间戳。

* oauth_nonce:单次值,一个随机字符串,防止重复攻击。

* oauth_version: OAuth协议版本。填写“1.0”。

生成的BaseString如下:

"POST&http%3A%2F%2Fapi.t.sohu.com%2Foauth%2Faccess_token&oauth_consumer_key%3D"+oauth_consumer_key+"%26"+

"oauth_nonce%3D"+noonce+"%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D"+time+"%26oauth_version%3D1.0"+

"%26x_auth_mode%3Dclient_auth%26x_auth_password%3D"+password+"%26x_auth_username%3D"+username;

3.根据oauth生成算法,.用刚刚生成的BaseString,经过HMAC-SHA1生成oauth_signature,注意这里的key是 consumer secret 后面加一个&,因为我们并没有oauth_token_secret,这是和oauth生成算法不一样的地方。生成key示例如下:

2u/xFErTc8vC1Z6R940HU3i5wAk=

URLEncode如下:
2u%2FxFErTc8vC1Z6R940HU3i5wAk%3D

4.根据以下参数向并向http://api.t.sohu.com/oauth/access_token接口提交POST请求:

·Httpheader 中的参数包括:

o oauth_consumer_key -

o oauth_nonce -

o oauth_signature_method - HMAC-SHA1

o oauth_timestamp - 1284565601

o oauth_version - 1.0

·HTTP post-body中包含的参数:

o x_auth_mode - client_auth

o x_auth_password – pwd

o x_auth_username – user_name

如下所示:

POST /oauth/access_token HTTP/1.1

Authorization: OAuth oauth_consumer_key="dbdP67NTrrNU0v2N6Yuu",oauth_version="1.0", oauth_signature_method="HMAC-SHA1",oauth_timestamp="1310372236", oauth_nonce="518611817947878737",oauth_signature="2u%2FxFErTc8vC1Z6R940HU3i5wAk%3D"

User-Agent: Java/1.6.0_13

Host: api.t.sohu.com

Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2

Connection: keep-alive

Content-type: application/x-www-form-urlencoded

Content-Length: 78

x_auth_username=---------&x_auth_password=------&x_auth_mode=client_auth

5. 得到的返回结果示例如下:

oauth_token=b3d804ee16d9d92820fb2f188203909a&oauth_token_secret=c2d687416889d575575ed8c2694a208b&user_id=100945678&screen_name=..&x_auth_expires=0


将返回结果按"&"拆开,oauth_token的值即为access_token。

6.如何使用access_token和oauth_token_secret来调用接口. 这里我们以查看用户详细信息为例子介绍。http://api.t.sohu.com/users/show.json

OAuthConsumer consumer = new DefaultOAuthConsumer(

"dbdP67NTrrNU0v2N6Yuu",consumer_secret);

// access_token和 access_token_secret consumer.setTokenWithSecret("b3d804ee16d9d92820fb2f188203909a","c2d687416889d575575ed8c2694a208b");

URL url = new URL("http://api.t.sohu.com/users/show.json");

HttpURLConnection request;

request = (HttpURLConnection)url.openConnection();

request.setDoOutput(true);

request.setRequestMethod("GET");

System.out.println("Sendingrequest...");

consumer.sign(request);

request.connect();

System.out.println("Response: " +request.getResponseCode() + " "

+ request.getResponseMessage());

BufferedReader reader =newBufferedReader(new InputStreamReader(request.getInputStream()));

String b = null;

while((b = reader.readLine())!=null){

System.out.println(b);

}

request.disconnect();

}

得到结果如下:

{"id":"100945678","screen_name":"未见见","name":"","location":"北京市,海淀区","description":"","url":"","profile_image_url":"http://s4.cr.itc.cn/mblog/icon/3d/6f/m_13080399324892.jpg","protected":true,"followers_count":62,"profile_background_color":"","profile_text_color":"","profile_link_color":"","profile_sidebar_fill_color":"","profile_sidebar_border_color":"","friends_count":213,"created_at":"MonMay 09 20:09:06 +08002011","favourites_count":1,"utc_offset":"","time_zone":"","profile_background_image_url":"","notifications":"","geo_enabled":false,"statuses_count":264,"following":true,"verified":false,"lang":"zh_cn","contributors_enabled":false}

HTTP 请求如下:

GET /users/show.json HTTP/1.1

Authorization: OAuthoauth_token="b3d804ee16d9d92820fb2f188203909a",oauth_consumer_key="dbdP67NTrrNU0v2N6Yuu", oauth_version="1.0",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1310437374",oauth_nonce="-4185729389431891046", oauth_signature="tEvVrQobzXoyJYqLq7qxApPQJks%3D"

User-Agent: Java/1.6.0_13

Host: api.t.sohu.com

Accept: text/html, image/gif,image/jpeg, *; q=.2, */*; q=.2

分享到:
评论

相关推荐

    XAuth OAuth认证类库

    XAuth、OAuth认证类库,内含类库源码、DLL、文档。

    IPSEC_XAUTH

    IPSEC_XAUTH产生背景、XAUTH在IKE中交互阶段、XAUTH报文字段说明;

    xorg-x11-xauth

    centos 的x11转发工具,用于转发xshell连接请求,避免出现连接缓慢,连接警告 The remote SSH server rejected X11 forwarding request

    xorg-x11-xauth-1.0.2-7.1.el6.x86_64.rpm

    xorg-x11-xauth-1.0.2-7.1.el6.x86_64.rpm

    xorg-x11-xauth-1.0.9-1.el7.x86_64.rpm

    离线安装包,亲测可用

    WP7新浪微博SDK源码

    支持 Oauth 1.0 和 xAuth 两种授权模式。 下载包中包含源代码,示例代码。 新浪微博是一个由新浪网推出,提供微型博客服务的类Twitter网站。 用户可以通过网页、WAP页面、手机短信、彩信发布消息或上传图片。 新浪...

    xauth命令 修改x服务器访问授权

    不使用默认的认证文件,而使用指定的认证文件 -q 安静模式,不打印未请求的状态信息 -v 详细模式,打印指定的各种操作信息 -i 忽略认证文件锁定 -b 执行任何操作,中断认证文件锁定 add 添加认证条目...

    koa-xauth-源码.rar

    koa-xauth-源码.rar

    omniauth-xauth

    创建 XAuth 策略要使用此 gem 创建 OmniAuth XAuth 策略,您可以简单地将其子类化并添加一些额外的方法,如下所示: require 'omniauth-xauth'module OmniAuth module Strategies class SomeSite ...

    koa-xauth:Node后端微服务框架,基于koa-xauth中间件,TOKEN令牌式身份认证集成

    koa-xauth Node后端微服务框架,基于koa-xauth中间件,TOKEN令牌式身份认证集成 框架目录结构 ├── app.js ├── config │ ├── default.json │ ├── develop.json │ └── production.json ├── ...

    LSXAuthSwift:Swift的xAuth Authorization标头生成器

    LSXAuthSwift是使用Instapaper API设计和测试的,目前,这是已知的唯一可以使用的xAuth API。 起源 当我尝试从swift项目连接到Instapaper API时,我花了很长时间才使它起作用。 我还没有找到任何用Swift编写的工作...

    insta-wrapper:Instagram API包装器允许使用xAuth进行非破坏性调用

    即时包装Instagram API包装器允许使用xAuth进行非破坏性调用。设置git clone https://github.com/plusk01/insta-wrapper.git npm install && bower install cp config/instagram.js.example config/instagram.js ...

    python使用xauth方式登录饭否网然后发消息

    开发环境:python版本2.X 复制代码 代码如下:#!/usr/bin/env python# -*- coding:utf-8 -*-# 适合python版本:2.X import sys, urllib, reimport oauth.oauth as oauthfrom urllib2 import Request, urlopen ...

    ubuntu 8.0.4 server 安装oracle10.2成功

    Install software 0. software oracle 10g for linux ... sudo apt-get install xauth libxp6 libxt6 libxtst6 sudo apt-get install x11-apps sudo apt-get install sun-java6-jdk 。。。。

    django-api-example:带有Django,Tastypie,xAuth和Heroku的示例REST API

    概述 此示例说明如何使用 , 和的变体创建... * 也使用xAuth。 原料 (和 )-我觉得自己好喜欢RESTEEP,而我喜欢Django,所以这是一个简单的起点。 此外,Tastypie通过django-oauth-plus应用程序支持OAuth(但不支持xA

    mashape-oauth:用于Node.js的OAuth模块-支持RSA,HMAC,PLAINTEXT,2,3-Legged,1.0a,Echo,XAuth和2.0

    Mashape OAuth 用于Node.js的OAuth模块-支持RSA,HMAC,PLAINTEXT,2足,3足,1.0a,Echo,XAuth和2.0OAuth圣经如果您正在寻找流行的OAuth圣经...使用OAuth(1.x,XAuth,Echo): var OAuth = require ( 'mashape-oau

    ScreenOS 5.3 第9 卷:用户认证

    第9 卷: 用户认证介绍 ScreenOS 中认证不同类型用户的方法。本卷介绍了用户认 证、可存储用户配置文件的两个位置 ( 内部数据库和外部认证服务器),然后提供 ...应用认证的策略中使用组表达式。 本卷包含以下章节:

    citrix-linuxvda-rhel7-1.2.0.tgz

    Xauthority 文件可以在每个登录用户的主目录中找到,用于在 xauth 使用的 cookie 中存储凭据用于 X 会话的身份验证。 启动 X 会话 后,该 cookie 将用于对与该特定显示的连接进行身份验证。 支持 IPv6 本版本支持 ...

    Linux_XForward.zip

    本工具包包含设置X11 Forward的详细指导和xauth的rpm离线包,以及让服务器支持运行VisualVM/JConsole等图形工具的离线rpm包,方便不能联网的服务器安装使用,按照本工具指导配置成功以后,可以让服务器运行图形监控...

    prosody-auth-OIDC:一个Prosody 0.10模块,允许使用OpenID Connect令牌登录,这太酷了!

    Prosody 0.10的此模块(不适用于0.9)允许用户使用XAuth令牌登录XMPP服务器。 密码字段必须包含OAUTH2令牌,该令牌已根据OIDC服务器的userinfo端点进行了验证。如何使用只需将lua文件复制到Prosody的modules目录中...

Global site tag (gtag.js) - Google Analytics