最近刚刚学习要调用微博用户好友列表,需要授权拿到code然后在拿access_token,和uid
为了防止自己忘记了特写此文章方便以后查看
首先要调用好友列表,必须他的好友也授权过该应用
java后端
链接:https://pan.baidu.com/s/1QKNO3f_kaQ12jNY1_8tmGA 密码:6uto
前端请求
链接:https://pan.baidu.com/s/18yzy9RSNb2GtQGvRAUfTpg 密码:uv77
首先必须拿到code,手动获取code可以get请求
https://api.weibo.com/oauth2/authorize?client_id=你的应用ID,&redirect_uri=应用回调地址
&response_type=code
https://api.weibo.com/oauth2/authorize?client_id=37831xxxx&redirect_uri=http://h5.xxx.com
&response_type=code
这个请求之后,就可以在回调的地址栏上看到code,测试的话为了方便可以手动获取,或者利用js之类的自动获取,把这个code发给后端,后端
<script>
$(function(){
$.ajax({
type:'POST',
url: 'http://h5.xxxxx.com:8888/uploadDemo/user/code',
data: {
code:'07c82de8e5d4fdf361cbdxxxxx'
},
success: function(data) {
alert(222);
console.log(data);
},
error: function(data) {
alert(111)
$('#mstr_ck').html("获取失败,请重试!");
}
})
})
</script>
后端写法
Controller类
package cn.linmu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@RequestMapping("/user")
@Controller
public class WbAuthController {
static String clientId="3783162400";//你的应用ID
static String clientSecret="dbe5c657c0fcde171a37b51445e8deb9";//你的应用密码
static String redirectUri="http://h5.linmutech.com";//你在应用管理中心设置的回调页面
@RequestMapping("/code")
@ResponseBody
public Object getcode(String code){
String url="https://api.weibo.com/oauth2/access_token";
String parameters="client_id=" +clientId+"&client_secret=" +clientSecret+
"&grant_type=authorization_code" +"&redirect_uri=" +redirectUri+"&code="+code;
Object a= Test.postUrl(url, parameters);
return a;
}
}
Test类。
package cn.linmu.controller;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Scanner;
import javax.net.ssl.X509TrustManager;
/**
* @author 刘显安
* 不使用任何SDK实现新浪微博Oauth授权并实现发微薄小Demo
* 日期:2012年11月11日
*/
public class Test
{
static String clientId="3xxxxxx";//你的应用ID
static String clientSecret="dbe5c657c0fcdxxxxxx51445e8deb9";//你的应用密码
static String redirectUri="http://h5.xxxx.com";//你在应用管理中心设置的回调页面
public static void main(String[] args) throws Exception
{
testHttps();//测试
//第一步:访问授权页面获取授权
System.out.println("请打开你的浏览器,访问以下页面,登录你的微博账号并授权:");
System.out.println("https://api.weibo.com/oauth2/authorize?client_id="+clientId+"&response_type=code&redirect_uri="+redirectUri+"&forcelogin=true");
//第二步:获取AccessToken
System.out.println("请将授权成功后的页面地址栏中的参数code:");
String code=new Scanner(System.in).next();
getAccessToken(code);
//第三步:发布一条微博
System.out.println("请输入上面返回的值中accessToken的值:");
String accessToken=new Scanner(System.in).next();
updateStatus("发布微博测试!来自WeiboDemo!", accessToken);
}
/**
* 测试能否正常访问HTTPS打头的网站,
*/
public static void testHttps()
{
try
{
trustAllHttpsCertificates();//设置信任所有的http证书
URL url=new URL("https://api.weibo.com/oauth2/default.html");
URLConnection con=url.openConnection();
con.getInputStream();
System.out.println("恭喜,访问HTTPS打头的网站正常!");
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 以Post方式访问一个URL
* @param url 要访问的URL
* @param parameters URL后面“?”后面跟着的参数
* @return
*/
public static Object postUrl(String url,String parameters)
{
try
{
trustAllHttpsCertificates();//设置信任所有的http证书
URLConnection conn = new URL(url).openConnection();
conn.setDoOutput(true);// 这里是关键,表示我们要向链接里注入的参数
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());// 获得连接输出流
out.write(parameters);
out.flush();
out.close();
// 到这里已经完成了,开始打印返回的HTML代码
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
return line;
}
}
catch (Exception e)
{
e.printStackTrace();
}
return parameters;
}
/**
* 获取AccessToken
* @param code 在授权页面返回的Code
*/
public static void getAccessToken(String code)
{
String url="https://api.weibo.com/oauth2/access_token";
String parameters="client_id=" +clientId+"&client_secret=" +clientSecret+
"&grant_type=authorization_code" +"&redirect_uri=" +redirectUri+"&code="+code;
postUrl(url, parameters);
}
/**
* 利用刚获取的AccessToken发布一条微博
* @param text 要发布的微博内容
*
* @param accessToken 刚获取的AccessToken
*/
public static void updateStatus(String text,String accessToken)
{
String url="https://api.weibo.com/2/statuses/update.json";
String parameters="status="+text+"&access_token="+accessToken;
postUrl(url, parameters);
System.out.println("发布微博成功!");
}
/**
* 设置信任所有的http证书(正常情况下访问https打头的网站会出现证书不信任相关错误,所以必须在访问前调用此方法)
* @throws Exception
*/
public static void trustAllHttpsCertificates() throws Exception
{
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
trustAllCerts[0] = new X509TrustManager()
{
@Override
public X509Certificate[] getAcceptedIssuers()
{
return null;
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException
{}
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException
{}
};
javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
}
这个是我模仿csdn的大佬的文章内写的
主要是post请求微博的https://api.weibo.com/oauth2/access_token来获取access_token和uid
有了这两个就随便调接口了
http://open.weibo.com/wiki/%E5%BE%AE%E5%8D%9AAPI
根据微博的api,需要怎么传参数就怎么传就可以了