腾讯微博oauth认证

腾讯微博api采用OAuth 1.0a进行授权认证,除了采用官方提供的php-sdk进行开发外还可以使用php语言本身的oauth扩展。
首先下载安装oauth扩展。
windows版本下载地址:http://downloads.php.net/pierre/php_oauth-1.1.1-dev-5.3-nts-vc9-x86.zip

linux版本使用pecl下载安装:

pecl install oauth

我们需要三个页面:ten.php是跳转页面,跳转到授权页面,back.php是回调地址,用户授权完成后会跳,demo.php演示页面,用得到的授权信息发送一条微博。

ten.php

<?php
$req_url = 'https://open.t.qq.com/cgi-bin/request_token';
$authurl = 'https://open.t.qq.com/cgi-bin/authorize';
$acc_url = 'https://open.t.qq.com/cgi-bin/access_token';
$api_url = 'http://open.t.qq.com/api/t/add';
$conskey = 'aaaaaae';
$conssec = 'aaaaaaa';
$oauth = new OAuth($conskey,$conssec,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
$oauth = disableSSLChecks();
$oauth->setNonce((rand()));
$oauth->setTimestamp(time());
$requestToken = $oauth->getRequestToken($req_url,'http://localhost/back.php');
$_SESSION['oauth_token']=$requestToken['oauth_token'];
$_SESSION['oauth_token_secret']=$requestToken['oauth_token_secret'];
header("Location: $authurl".'?oauth_token='.$requestToken['oauth_token']);

back.php

<?php
$req_url = 'https://open.t.qq.com/cgi-bin/request_token';
$authurl = 'https://open.t.qq.com/cgi-bin/authorize';
$acc_url = 'https://open.t.qq.com/cgi-bin/access_token';
$api_url = 'http://open.t.qq.com/api/t/add';
$conskey = 'aaaaaae';
$conssec = 'aaaaaaa';
session_start();
$oauth = new OAuth($conskey,$conssec,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
$oauth = disableSSLChecks();
$oauth->setNonce(md5(rand()));
$oauth->setToken($reqtoken,$_SESSION['oauth_token_secret']);
$accesstoken = $oauth->getAccessToken($acc_url,null,$oauth_verfy);
//print_r($accesstoken);
$_SESSION['access_token'] = $accesstoken['access_token'];
$_SESSION['access_token_secret'] = $accesstoken['access_token_secret'];

demo.php

<?php
$req_url = 'https://open.t.qq.com/cgi-bin/request_token';
$authurl = 'https://open.t.qq.com/cgi-bin/authorize';
$acc_url = 'https://open.t.qq.com/cgi-bin/access_token';
$api_url = 'http://open.t.qq.com/api/t/add';
$conskey = 'aaaaaae';
$conssec = 'aaaaaaa';
session_start();
$oauth = new OAuth($conskey,$conssec,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
$oauth = disableSSLChecks();
$oauth->setNonce(md5(rand()));
$oauth->setToken($accesstoken['access_token'],$accesstoken['access_token_secret']);
$oauth->fetch($api_url,array('content'=>'pcre oauth test!','format'=>'json','clientip'=>'127.0.0.1'),OAUTH_HTTP_METHOD_POST);
echo $oauth->getLastResponse();
2011年11月26日 | 归档于 PHP/Python/C
标签:
本文目前尚无任何评论.

发表评论

XHTML: 您可以使用这些标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>