官方网站上面提供的Sdk是好好的,但是教程是严重滞后,使用的教程还是Oauth1.0的,2.0的教程网上介绍的也不多,前一段搜到郭少瑞分享的源代码程序,但是怎么也执行不了,后来仔细梳理了一下,发现他的方法和官网提供的借口调用友一个冲突的地方,只有请求授权,没有获取授权Access Token的步骤,所以自己重新写了一个Demo供大家参考。

据日的API文档和链接,大家可以去新浪官网里面查找,以下用到的URL定式,以及返回值的截取的规则你也可以根据官方文档自己总结。

链接地址:http://open.weibo.com/wiki/%E6%8E%88%E6%9D%83%E6%9C%BA%E5%88%B6%E8%AF%B4%E6%98%8E

注意:因为官方Sdk里面使用了Json,所以我们不要调用第三方Json包库,否则会发生冲突,发送微博函数中,

mb.callWeiboAPI("2/statuses/update",{"status":message,"access_token":accessToken},"POST","callWeiboApiResult","callWeiboApiError");

会报错,提示Json dismatch

代码如下:

package
{import com.sina.microblog.MicroBlog;import flash.display.Sprite;import flash.display.StageAlign;import flash.display.StageScaleMode;import flash.events.Event;import flash.events.LocationChangeEvent;import flash.geom.Rectangle;import flash.media.StageWebView;import flash.net.URLLoader;import flash.net.URLRequest;public class TestSinaWeibo extends Sprite{//APP IDprivate var appKey:String = "你的App Id";//APP 秘钥private var appSecret:String = "你的 App 密钥";//新浪微薄接口封装private var mb:MicroBlog;//令牌private var accessToken:String;//微博信息private var message:String = "这是我发送的第一条成功的测试微博";public function TestSinaWeibo(){super();stage.align = StageAlign.TOP_LEFT;stage.scaleMode = StageScaleMode.NO_SCALE;//初始化新浪微博封装对象if(mb == null){mb = new MicroBlog();mb.consumerKey = appKey;mb.consumerSecret = appSecret;}if(accessToken != null){mb.access_token = accessToken;sendNewWeibo();}else{//使用webView提请授权Tokenvar url:String = "https://api.weibo.com/oauth2/authorize";url += "?client_id=" + appKey;url += "&redirect_uri=http://www.tech-mx.com";url += "&display=mobile";
//                url += "&response_type=code";var viewWidth:int = stage.stageWidth;var viewHeight:int = stage.stageHeight;var webView:StageWebView = new StageWebView();webView.stage = stage;webView.viewPort = new Rectangle(0,0,viewWidth,viewHeight);webView.loadURL(url);webView.addEventListener(Event.COMPLETE,onComplete);}}protected function onComplete(event:Event):void{//获取授权链接返回值lcvar myWebView:StageWebView = event.target as StageWebView;var lc:String = myWebView.location;trace("url:"+lc);//从返回值lc获取Codevar code:String = lc.slice(lc.indexOf("=")+1,lc.length);trace("Code:"+code);if(lc.indexOf("code=")>=0){ //评比非正常返回链接情况//获取请求授权后,获取授权过的Access Tokenvar url:String = "https://api.weibo.com/oauth2/access_token";url += "?client_id=" + appKey;url += "&client_secret=" + appSecret;url += "&grant_type=authorization_code";url += "&code="+code;url += "&redirect_uri=http://www.tech-mx.com";var ur:URLRequest = new URLRequest(url);ur.method = "POST"; //必须是POST方法var ul:URLLoader = new URLLoader(ur);ul.addEventListener(Event.COMPLETE,ulOnComplete);}}protected function ulOnComplete(event:Event):void{var ul:URLLoader = event.target as URLLoader;var str:String = ul.data; //获取返回值Access Tokentrace("json:"+str);accessToken = str.slice(str.indexOf(":\"")+2,str.indexOf("\",")); //获取Access Tokentrace("accessToken:"+accessToken);sendNewWeibo();}private function sendNewWeibo():void{mb.addEventListener("callWeiboApiResult",callWeiboApiResultHandler);mb.addEventListener("callWeiboApiError",callWeiboApiResultHandler);mb.callWeiboAPI("2/statuses/update",{"status":message,"access_token":accessToken},"POST","callWeiboApiResult","callWeiboApiError");}/**调用API的结果*/private function callWeiboApiResultHandler(event:Event):void{if(event.type == "callWeiboApiResult"){trace("发布成功");}else{trace("发布失败");}}}
}

转载于:https://www.cnblogs.com/flashbird/p/3346542.html