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

发布于 2022-09-01 18:24:57 字数 2972 浏览 56 评论 0

已经取得了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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

佼人 2022-09-08 18:24:57

你解决了么?

烛影斜 2022-09-08 18:24:57

同求 这个接口我直接arc访问了确实是要走post 走get返回不支持get方法
试过了下别人说的jsonp跨域 但是 jsonp不支持post

抱着落日 2022-09-08 18:24:57

解决了吗 ,大神?

去了角落 2022-09-08 18:24:57

请问你解决了吗?我也在做这块 接受状态对着呢 就是收不到返回值?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文