首先阅读官方文档
申请接入网站或者应用
得到相应的App Key和App Secret
如果应用或网站未通过审核,请添加测试微博账号
首先新浪微博关于授权写的不是很仔细,小弟经过仔细给出C#.net获取access_token例子
其他思路是一样的。首先申请网站接入的时候填写的接入站点域名例如www.baidu.com
首先获取access_token不支持本地调试,之能发布后使用例如放到www.baidu.com站点下面
整个微博授权都不支持本地调试,请发布后调试
采用最简单的一般处理程序
GetWBAccess_token.ashx代码如下
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
string client_id = context.Request.QueryString["client_id"];
string client_secret = context.Request.QueryString["client_secret"];
string code = context.Request.QueryString["code"];
string redirect_uri = context.Request.QueryString["redirect_uri"];
string results = getWBaccess_token(client_id, client_secret, code, redirect_uri);
context.Response.Write(results);
}
public bool IsReusable
{
get
{
return false;
}
}
public string getWBaccess_token(string client_id, string client_secret, string code, string redirect_uri)
{
string url = "https://api.weibo.com/oauth2/access_token?client_id=" + client_id + "&client_secret=" + client_secret + "&grant_type=authorization_code&redirect_uri=" + redirect_uri + "&code=" + code; ;
string ReqStr = RequestPostDataUrl(url, string.Empty);
return ReqStr;
}
#region Post请求服务器数据
public string RequestPostDataUrl(string url, string strPostDatas)
{
string strResponse = "";
string targeturl = url.Trim().ToString();
//如果是发送HTTPS请求
HttpWebRequest Request = null;
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
Request = WebRequest.Create(url) as HttpWebRequest;
Request.ProtocolVersion = HttpVersion.Version10;
}
else
{
Request = WebRequest.Create(url) as HttpWebRequest;
}
//HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(targeturl);
Request.Method = "POST";
Request.KeepAlive = true;
Request.ContentType = "application/x-www-form-urlencoded";
byte[] postDatas = null;
if (strPostDatas != null && strPostDatas.Length > 0)
{
postDatas = Encoding.GetEncoding("utf-8").GetBytes(strPostDatas);
Request.ContentLength = postDatas.Length;
using (Stream reqStream = Request.GetRequestStream())
{
reqStream.Write(postDatas, 0, postDatas.Length);
}
}
HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
using (Stream resStream = Response.GetResponseStream())
{
using (StreamReader sr = new StreamReader(resStream, System.Text.Encoding.GetEncoding("utf-8")))
{
strResponse = sr.ReadToEnd();
}
}
return strResponse;
}
#endregion
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true; //总是接受
}
html获取代码如下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wb="http://open.weibo.com/wb">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>微博分享测试</title>
<script src="Scripts/jquery-1.11.1.min.js" type="text/javascript"></script>
<script src="http://tjs.sjs.sinajs.cn/open/api/js/wb.js?appkey=2746973445&debug=true" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
var client_id = "你的Appkey";
var client_secret = "App Secret";
var wb_userinfo;
//微博授权登录
function wb_authorize(appkey, redirect_uri) {
url = "https://api.weibo.com/oauth2/authorize?client_id=" + appkey + "&response_type=code&redirect_uri=" + redirect_uri + "&display=mobile&scope=all";
window.location.href = url;
}
//微博获取access_token
function getwb_access_token(redirect_uri) {
var code = getParam("code");
alert(code);
url = "http://您的站点接口地址/GetWBAccess_token.ashx?client_id=“+client_id +”&client_secret=“+client_secret+”&redirect_uri=“+redirect_uri+”&code=" + code;
var data = getData("get",url, null);
alert(data);
alert(data.access_token);
alert(data.expires_in);
alert(data.remind_in);
alert(data.uid);
}
function getParam(paramName) {
paramValue = "";
isFound = false;
if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) {
arrSource = unescape(this.location.search).substring(1, this.location.search.length).split("&");
i = 0;
while (i < arrSource.length && !isFound) {
if (arrSource[i].indexOf("=") > 0) {
if (arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase()) {
paramValue = arrSource[i].split("=")[1];
isFound = true;
}
}
i++;
}
}
return paramValue;
}
function getData(method, urlStr, argu) {
var xmlhttp = new XMLHttpRequest(),
data = "";
argu = argu || null;
xmlhttp.onload = function (e) {
data = JSON.parse(xmlhttp.responseText);
}
xmlhttp.onerror = function (e) {
console.log(e);
}
xmlhttp.open(method, urlStr, false);
xmlhttp.send(argu);
return data;
}
</script>
</head>
<body>
<input id="btn_wb_authorize" οnclick="wb_authorize('您的appkey','回调地址')" type="button" value="微博授权"/>
<input id="btn_wb_access_token" οnclick="getwb_access_token(‘回调地址’)" type="button" value="获取access_token"/>
</body>
</html>
如果还有疑问请联系@boy-unknow新浪微博