我已经获得code了,但是换取accesstoken时,(5个参数我都写了,用AFN发送的POST请求)返回就是
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unauthorized (401)"
求高手解决???
新浪微博开放平台用code换取accesstoken不成功,错误代码401,请问怎么解决?
答案:2 悬赏:60
解决时间 2021-03-01 18:42
- 提问者网友:傲气稳全场
- 2021-02-28 19:40
最佳答案
- 二级知识专家网友:茫然不知崩溃
- 2021-02-28 20:36
应该是你的client_ID和client_SERCRET配置错了吧,再次确认一下,注意空格什么的。如果还是不行的话,在微博开放平台中的我的应用的栏的网站信息->测试信息中添加测试账号试试。
全部回答
- 1楼网友:转身→时光静好
- 2021-02-28 21:29
希望能帮到你:
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(),"测试呢");
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯