中易网

新浪微博开放平台用code换取accesstoken不成功,错误代码401,请问怎么解决?

答案:2  悬赏:60  
解决时间 2021-03-01 18:42
我已经获得code了,但是换取accesstoken时,(5个参数我都写了,用AFN发送的POST请求)返回就是
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unauthorized (401)"
求高手解决???
最佳答案
应该是你的client_ID和client_SERCRET配置错了吧,再次确认一下,注意空格什么的。如果还是不行的话,在微博开放平台中的我的应用的栏的网站信息->测试信息中添加测试账号试试。
全部回答
希望能帮到你: package weibo4j.examples.timeline; import org.apache.commons.httpclient.header; import org.apache.commons.httpclient.httpclient; import org.apache.commons.httpclient.httpexception; import org.apache.commons.httpclient.methods.postmethod; import org.apache.commons.httpclient.params.httpmethodparams; import weibo4j.oauth; import weibo4j.timeline; import weibo4j.http.accesstoken; import weibo4j.model.weiboexception; import weibo4j.util.weiboconfig; import java.io.ioexception; import java.util.arraylist; import java.util.list; public class sinaweiboul { public static accesstoken gettoken(string username,string password) throws httpexception, ioexception{ string clientid = weiboconfig.getvalue("client_id") ; string redirecturi = weiboconfig.getvalue("redirect_uri") ; string url = weiboconfig.getvalue("authorizeurl"); postmethod postmethod = new postmethod(url); //应用的app key postmethod.addparameter("client_id",clientid); //应用的重定向页面 postmethod.addparameter("redirect_uri",redirecturi); //模拟登录参数 //开发者或测试账号的用户名和密码 postmethod.addparameter("userid", username); postmethod.addparameter("passwd", password); postmethod.addparameter("isloginsina", "0"); postmethod.addparameter("action", "submit"); postmethod.addparameter("response_type","code"); httpmethodparams param = postmethod.getparams(); param.setcontentcharset("utf-8"); //添加头信息 list
headers = new arraylist
(); headers.add(new header("referer", "https://api.weibo.com/oauth2/authorize?client_id="+clientid+"&redirect_uri="+redirecturi+"&from=sina&response_type=code")); headers.add(new header("host", "api.weibo.com")); headers.add(new header("user-agent","mozilla/5.0 (windows nt 6.1; rv:11.0) gecko/20100101 firefox/11.0")); httpclient client = new httpclient(); client.gethostconfiguration().getparams().setparameter("http.default-headers", headers); client.executemethod(postmethod); int status = postmethod.getstatuscode(); system.out.println(status); if (status != 302) { system.out.println("token刷新失败"); return null; } //解析token header location = postmethod.getresponseheader("location"); if (location != null) { string returl = location.getvalue(); int begin = returl.indexof("code="); if (begin != -1) { int end = returl.indexof("&", begin); if (end == -1) end = returl.length(); string code = returl.substring(begin + 5, end); if (code != null) { oauth oauth = new oauth(); try{ accesstoken token = oauth.getaccesstokenbycode(code); return token; }catch(exception e){ e.printstacktrace(); } } } } return null; } public static boolean sinasendweibo(string token,string content) throws exception { boolean flag = false ; timeline timeline = new timeline(); timeline.client.settoken(token); try { timeline.updatestatus(content); flag = true ; } catch (weiboexception e) { flag = false ; system.out.println(e.geterrorcode()); } return flag; } public static void main(string[] args) throws exception { accesstoken at = gettoken("用户","密码"); system.out.println(at.getaccesstoken()); sinasendweibo(at.getaccesstoken(),"测试呢"); } }
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
黑白相间的外衣和白色的鞋子要配什么颜色的裤
乳酸依沙吖啶溶液的作用
父母得了癌症,意味着自己离癌症也不远了吗
多个进程可以(通过socket)同时使用同一个端口
查企业的网站有哪些?
三极管的电流时怎么流的
vivo x6微信扣扣聊天界面怎么横屏
Valentino铆钉鞋会不会很容易过时?
在平行四边形ABCD中,AE,AF分别平分角DAB和角A
工作十年了 现在公司要求托业成绩,听说扬格
微信公众号绑定对公账户,没有怎么办
QQ等级几个星星才能变成一个月亮
地下停车场用英语怎么说
哲学上把本能定义为什么?
广字头加卢读什么字
推荐资讯
5.9m宽带是指多少
求小学生上下课口号!比如好好学习天天向上类
华清驾校大庆路报名联络点在哪里啊,我有事要
信阳炖菜馆我想知道这个在什么地方
求赛尔号好号和密码
九等分圆怎么画
新天地家园地址有知道的么?有点事想过去
学生保险报销比例多少摔伤
蓝秀和温碧泉那个补水效果好呀?
土地拍卖缴税
两凸透镜的焦距分别为f1和f2,且f1>f2,它们
安妮文艺生活驿站这个地址在什么地方,我要处
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?