App Key、App Secret、設定 OAuth2.0 授權回調頁(開發期可填 https://www.baidu.com)src(核心庫)與 example(範例)修改 src/config.properties:
client_ID=您的App Key
client_SERCRET=您的App Secret
redirect_URI=您的授權回調頁URL
文檔 → API → API測試工具 → 選擇應用 → 獲取 Access Token,直接複製生成的 Token 用於測試,跳過 OAuth 流程。
public class WeiboDemo {
static final String CLIENT_ID = "您的App Key";
static final String CLIENT_SECRET = "您的App Secret";
static final String REDIRECT_URI = "您的回調頁";
public static void main(String[] args) throws Exception {
// 1. 導向授權頁
System.out.println("請開啟瀏覽器訪問:");
System.out.println("https://api.weibo.com/oauth2/authorize?client_id=" + CLIENT_ID +
"&response_type=code&redirect_uri=" + REDIRECT_URI + "&forcelogin=true");
// 2. 手動輸入授權碼
Scanner sc = new Scanner(System.in);
System.out.print("請輸入授權後 URL 中的 code 參數:");
String code = sc.nextLine();
// 3. 換取 Access Token
String tokenUrl = "https://api.weibo.com/oauth2/access_token";
String params = "client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET +
"&grant_type=authorization_code&redirect_uri=" + REDIRECT_URI + "&code=" + code;
String tokenResp = postHttps(tokenUrl, params);
System.out.println("Token 回應:" + tokenResp);
// 解析 JSON 取得 access_token(建議用 Jackson/Gson)
// 4. 發布微博
System.out.print("請輸入 access_token:");
String accessToken = sc.nextLine();
String postUrl = "https://api.weibo.com/2/statuses/update.json";
String postParams = "status=測試微博來自Java Demo&access_token=" + accessToken;
String postResp = postHttps(postUrl, postParams);
System.out.println("發布結果:" + postResp);
}
static String postHttps(String url, String params) throws Exception {
trustAllCerts(); // 忽略憑證驗證(僅測試用)
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
try (OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream())) {
wr.write(params);
}
try (BufferedReader rd = new BufferedReader(
new InputStreamReader(conn.getInputStream()))) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) sb.append(line);
return sb.toString();
}
}
static void trustAllCerts() throws Exception {
TrustManager[] trustAll = new TrustManager[]{
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() { return null; }
public void checkClientTrusted(X509Certificate[] c, String a) {}
public void checkServerTrusted(X509Certificate[] c, String a) {}
}
};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAll, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
}
| 功能 | HTTP 方法 | 端點 | 關鍵參數 |
|---|---|---|---|
| 授權頁 | GET | /oauth2/authorize | client_id, response_type=code, redirect_uri |
| 換 Token | POST | /oauth2/access_token | client_id, client_secret, grant_type, code, redirect_uri |
| 發微博 | POST | /2/statuses/update.json | status, access_token |
| 用戶時間線 | GET | /2/statuses/user_timeline.json | access_token, uid/screen_name |
/oauth2/access_token 配合 grant_type=refresh_token)trustAllCerts(),需正確配置信任存儲error_code、error 欄位,對應錯誤碼表處理App Secret 嚴禁前端暴露,Token 應加密儲存