本文适合没有通过审核新浪微博应用又想使用调用微博API的朋友,因为开发者的access_token
有效期为5年,所以只要我们获得token就可以进行获取微博或者同步文章之类的功能了。
将下面的代码保存为get_sina_token.php
,然后把你的应用信息填进去,再上传到网站根目录
查看appkey
和appsecret
设置回调地址,设置为你的域名/get_sina_token.php
,否则会出现认证错误。
<?php $appkey = "**********";//App Key $appsecret ="**********";//App Secret $redirect_url = "http://example.com";//授权回调页,只需填写首页地址即可 $redirect_url .= "/get_sina_token.php"; $login_url = "https://api.weibo.com/oauth2/authorize?client_id=" . $appkey . "&response_type=code&redirect_uri=" . urlencode ($redirect_url); function do_post($url, $data) { $ch = curl_init (); $header[] = "Accept: application/json"; $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, TRUE ); curl_setopt ( $ch, CURLOPT_POST, TRUE ); curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data ); curl_setopt ( $ch, CURLOPT_URL, $url ); curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE); $ret = curl_exec ( $ch ); curl_close ( $ch ); return $ret; } ?><!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>获取新浪微博access_token</title> </head> <body> <?php if (isset($_GET['code'])){ $code = $_GET['code']; $url = "https://api.weibo.com/oauth2/access_token"; $data = "client_id=" . $appkey . "&client_secret=" . $appsecret . "&grant_type=authorization_code&redirect_uri=".urlencode ($redirect_url)."&code=".$code; $output = json_decode(do_post($url,$data)); $sina_access_token = $output->access_token; $sina_uid = $output->uid; echo 'uid:'.$sina_uid.'<br/>'; echo 'token:'.$sina_access_token; } else { ?> <a href="<?php echo $login_url;?>">授权</a> <?php } ?> </body> </html>
然后访问该文件
点击授权即可查看到你的access_token
拥有多年WordPress博客/企业建站开发经验,独树一帜的设计,提供最专业的网站设计方案;原创的主题标签语义化,增强网站优化效果。
增强用户体验、提高品牌形象给人留下深刻的印象,那就选择我们,我们的每一款主题都是独一无二的,都有自己的灵魂,每款主题都有各自的配色方案。
遵循现代网页设计的趋势,所有主题都提供完整的响应式布局,优化大桌面显示器以及对平板和智能手机的支持,从而提供一致性的用户体验。
长期及时地在线售后服务和免费升级更新,确保您的网站始终运行在最佳状态,24小时QQ在线的沟通方式免去您的后顾之忧,替您节省网站维护成本。
目前还没有评。