在新浪微博快速登录时,获得code之后如何发送请求获得token?


已经取得了code,整理后的url如下:
var url = ' https://api.weibo.com/oauth2/access_token?client_id= '+CONFIG['wb_appid']+'&client_secret='+CONFIG['wb_secret']+'&grant_type=authorization_code&redirect_uri='+getCurrLocation+'&code='+obj[key];
根据 http://open.weibo.com/wiki/Oauth2/access_token 所说,请求需要post方式。做了两次试验代码如下(不考虑兼容性):
第一个:在index.html文件中加入了
<meta http-equiv="Access-Control-Allow-Origin" content="*" />


 var xhr = new XMLHttpRequest;
   xhr.open("post", url , true);
   xhr.setRequestHeader("Content-Type", "text/html;charset=utf-8");
   xhr.onreadystatechange = function () {
       if (xhr.readyState === 4 && xhr.status === 200) {
           var text = xhr.responseText;
           var ary = JSON.parse(text);
           console.log(ary);
       }
   };
   xhr.send();

报错:
POST https://api.weibo.com/oauth2/access_token $.ajax @ zepto.extend.js:1546(anonymous function)

index.html?code=504515c6743499c94141d66191ea0f11:1 XMLHttpRequest cannot load https://api.weibo.com/oauth2/access_token. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost :63342' is therefore not allowed access. The response had HTTP status code 401.
第二个:


 $.ajax({
           type: "post",
           url: "https://api.weibo.com/oauth2/access_token",
           data: obj,
           dataType: "jsonp crossdomain:true",
           success: function(data){
            console.log(data);
           },
           error:function(data){
               console.log(data);
           }
       });

其中obj


 var obj ={
           grant_type:'authorization_code',
           client_id:CONFIG['wb_appid'],
           client_secret:CONFIG['wb_secret'],
           redirect_uri:getCurrLocation(),
           code:obj[key]
       };

报错:XMLHttpRequest cannot load https://api.weibo.com/oauth2/access_token?client_id=1234567890&client_secre …0;%20%20%20%20%20%20%20%20%20%20%20}&code=9000d5a0f26de70042fbdaf465653221f1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost :63342' is therefore not allowed access.

请大神们指导一下,该如何做?

token code 新浪微博 跨域 快速登录

rabket 10 years, 3 months ago

Your Answer