首页 > PHP > 腾讯微博PHP OAuth授权

腾讯微博PHP OAuth授权

之前研究过新浪微博OAuth授权,刚巧腾讯微博最近也开放API了.

本想使用Ruby OAuth来进行授权,只是每次在获取AcessToken的时候,都出现签名错误。
问题耽搁了很久,且一直未能解决,最后是直接使用了官方的PHP SDK来绕过了,并结合需求,实现了两个功能:
1.授权成功的同时,绑定到自家网站的用户id,没有就生成一个.
2.将PHP的发微博功能封装成了一个API,供自家调用.

authorize.php <!-- 这里是OAuth入口 -->
<?php
  @header('Content-Type:text/html;charset=utf-8');
  session_start();
  require_once('config.php'); //配置AppKey, AppSecret
  require_once('oauth.php');
  require_once('opent.php');
  $o = new MBOpenTOAuth( MB_AKEY , MB_SKEY );
  $keys = $o->getRequestToken('http://api.5yi.com/qq/callback.php');//这里填上你的回调URL
  $aurl = $o->getAuthorizeURL( $keys['oauth_token'] ,false,'');
  $_SESSION['keys'] = $keys;
  header("Location: ".$aurl);
?> callback.php
<!-- OAuth回调地址,绑定我易网用户 -->
<?php
  @header('Content-Type:text/html;charset=utf-8');
  session_start();
  @require_once('config.php');
  @require_once('oauth.php');
  @require_once('opent.php');
  @require_once('api_client.php');
  // 获取Acess Token
  $o = new MBOpenTOAuth( MB_AKEY , MB_SKEY , $_SESSION['keys']['oauth_token'] , $_SESSION['keys']['oauth_token_secret'] );
  $last_key = $o->getAccessToken( $_REQUEST['oauth_verifier'] );
  $_SESSION['last_key'] = $last_key;
  // 获取用户的信息
  $c = new MBApiClient( MB_AKEY , MB_SKEY , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret'] );
  $response = $c->getUserInfo();
  // 重定向到我易网登录API,绑定我易网用户id
  $url = "http://api.5yi.com:9080/third_login/t_qq?token=".$last_key['oauth_token']."&secret=".$last_key['oauth_token_secret']."&uid=".$response['data']['name']."&uname=".$response['data']['nick']."&source=tqq";
  header("Location: ".$url);
?>
post.php
<!-- 调用PHP接口,发送微博信息 -->
<?php
@header('Content-Type:text/html;charset=utf-8');
session_start();
@require_once('config.php');
@require_once('oauth.php');
@require_once('opent.php');
@require_once('api_client.php');
//$c = new MBApiClient( MB_AKEY , MB_SKEY , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret'] );
$c = new MBApiClient( MB_AKEY , MB_SKEY , $_REQUEST['oauth_token'] , $_REQUEST['oauth_token_secret'] );
//发消息
$p =array(
'c' => ''.$_REQUEST["text"],
'ip' => $_SERVER['REMOTE_ADDR'],
'j' => '',
'w' => ''
);
var_dump($c->postOne($p));
?>
分类:PHP 标签:, ,
  1. 还没有评论。
  1. No trackbacks yet.

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: