Home Database Mysql Tutorial 新浪微博Oauth2.0授权 获取Access Token以及API的使用

新浪微博Oauth2.0授权 获取Access Token以及API的使用

Jun 07, 2016 pm 03:50 PM
access Weibo Authorize Sina Obtain

今天需要使用新浪微博提供的API获取微博的详细信息以及用户的详细信息,所以需要使用API中的/statuses/show接口。 使用新浪微博API的前提是需要到新浪微博开放平台注册一个应用,获得App Key和App Secret,然后再根据这两个获取Access Token。下面详细介绍如

今天需要使用新浪微博提供的API获取微博的详细信息以及用户的详细信息,所以需要使用API中的/statuses/show接口。

使用新浪微博API的前提是需要到新浪微博开放平台注册一个应用,获得App Key和App Secret,然后再根据这两个获取Access Token。下面详细介绍如何获取Access Token以及如何使用新浪微博提供的API。

一、获取Access Token

1、进入新浪微博开放平台,进入应用开发,选择创建应用,这里我选择的是“网页应用”

新浪微博Oauth2.0授权 获取Access Token以及API的使用

2、填写应用的基本信息

可以随便填写,点击“创建”,就可以创建成功!新浪微博Oauth2.0授权 获取Access Token以及API的使用

创建成功后,可以查看应用的信息。主要需要的是App Key和App Secret。

新浪微博Oauth2.0授权 获取Access Token以及API的使用

3、添加授权回调页

选择刚刚创建的应用,进入到应用信息,选择高级信息,添加授权回调页。

这个一定要添加,如果没有授权回调页,是无法获取到Access Token 的,而且必须保证回调页的网址时可以访问的。

新浪微博Oauth2.0授权 获取Access Token以及API的使用

4、获取新浪微博SDK包

在新浪微博开放平台,选择文档->资源下载->SDK下载,我选择Java SDK的版本。下载完成后,解压缩,将项目导入到Eclipse中。

可以查看一下文档:readme.txt,内容为:

[plain] view plaincopyprint?

  1. 请先填写相关配置:在Config.properties里  
  2. client_ID :appkey                             
  3. client_SERCRET :app_secret  
  4. redirect_URI : 回调地址  
即运行项目的前提需要进行配置。

5、配置SDK开发包

打开Config.properties文件(在src目录下)

[plain] view plaincopyprint?

  1. client_ID =   
  2. client_SERCRET =   
  3. redirect_URI =   
  4. baseURL=https://api.weibo.com/2/  
  5. accessTokenURL=https://api.weibo.com/oauth2/access_token  
  6. authorizeURL=https://api.weibo.com/oauth2/authorize  
  7. rmURL=https://rm.api.weibo.com/2/  
需要你填写client_ID、client_SERCRET、redirect_URI ,这一定要与你的应用相对应。格式和下方的相同。不要加双引号,不要在结尾加分号。

6、获取Access Token

打开example资源文件夹下,weibo4j.examples.oauth2文件夹下的OAuth4Code.java

新浪微博Oauth2.0授权 获取Access Token以及API的使用

将代码:

[plain] view plaincopyprint?

  1. BareBonesBrowserLaunch.openURL(oauth.authorize("code",args[1],args[2]));  
  2. System.out.println(oauth.authorize("code",args[1],args[2]));  
修改为:

[plain] view plaincopyprint?

  1. BareBonesBrowserLaunch.openURL(oauth.authorize("code","",""));  
  2. System.out.println(oauth.authorize("code","", ""));  
即除去args参数。

然后选择run as Java Application,打开了浏览器,即打开的是你填写的回调地址,注意查看此时的url,最后一部分是code=XXXXXXXXXXXXXXXXXXXXX。复制这个code的值。查看Eclipse的控制台窗口:

 新浪微博Oauth2.0授权 获取Access Token以及API的使用
输入刚刚获得的code值,点击回车。

新浪微博Oauth2.0授权 获取Access Token以及API的使用

这时已经获得了access_token值和uid的值,现在你就可以使用access_token来运行example中的程序了。注意,access_token的值有一定的时效性,access_token失效后需要重新申请一下,即重复第6步,每次access_token的值是不会改变的。

二、运行example实例程序

我选择的是weibo4j.examples.timeline中的ShowStatus.java。将access_token换成刚才得到的值,id指的是微博的ID,运行,即可得到有关微博的信息以及用户的信息。

新浪微博Oauth2.0授权 获取Access Token以及API的使用

其他的程序也是相同的道理。


http://blog.csdn.net/xyw_eliot/article/details/8907918

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to disable background applications in Windows 11_Windows 11 tutorial to disable background applications How to disable background applications in Windows 11_Windows 11 tutorial to disable background applications May 07, 2024 pm 04:20 PM

1. Open settings in Windows 11. You can use Win+I shortcut or any other method. 2. Go to the Apps section and click Apps & Features. 3. Find the application you want to prevent from running in the background. Click the three-dot button and select Advanced Options. 4. Find the [Background Application Permissions] section and select the desired value. By default, Windows 11 sets power optimization mode. It allows Windows to manage how applications work in the background. For example, once you enable battery saver mode to preserve battery, the system will automatically close all apps. 5. Select [Never] to prevent the application from running in the background. Please note that if you notice that the program is not sending you notifications, failing to update data, etc., you can

How to convert deepseek pdf How to convert deepseek pdf Feb 19, 2025 pm 05:24 PM

DeepSeek cannot convert files directly to PDF. Depending on the file type, you can use different methods: Common documents (Word, Excel, PowerPoint): Use Microsoft Office, LibreOffice and other software to export as PDF. Image: Save as PDF using image viewer or image processing software. Web pages: Use the browser's "Print into PDF" function or the dedicated web page to PDF tool. Uncommon formats: Find the right converter and convert it to PDF. It is crucial to choose the right tools and develop a plan based on the actual situation.

How to read dbf file in oracle How to read dbf file in oracle May 10, 2024 am 01:27 AM

Oracle can read dbf files through the following steps: create an external table and reference the dbf file; query the external table to retrieve data; import the data into the Oracle table.

How does the Java reflection mechanism modify the behavior of a class? How does the Java reflection mechanism modify the behavior of a class? May 03, 2024 pm 06:15 PM

The Java reflection mechanism allows programs to dynamically modify the behavior of classes without modifying the source code. By operating the Class object, you can create instances through newInstance(), modify private field values, call private methods, etc. Reflection should be used with caution, however, as it can cause unexpected behavior and security issues, and has a performance overhead.

Common exception types and their repair measures in Java function development Common exception types and their repair measures in Java function development May 03, 2024 pm 02:09 PM

Common exception types and their repair measures in Java function development During the development of Java functions, various exceptions may be encountered, which affect the correct execution of the function. The following are common exception types and their repair measures: 1. NullPointerException Description: Thrown when accessing an object that has not been initialized. Fix: Make sure you check the object for non-null before using it. Sample code: try{Stringname=null;System.out.println(name.length());}catch(NullPointerExceptione){

How to cross-domain iframe in vue How to cross-domain iframe in vue May 02, 2024 pm 10:48 PM

Ways to solve iframe cross-domain issues in Vue: CORS: Enable CORS support in the backend server and use XMLHttpRequest or fetch API to send CORS requests in Vue. JSONP: Dynamically load JSONP scripts in Vue using the JSONP endpoint in the backend server. Proxy server: Set up a proxy server to forward requests, use a third-party library (such as axios) in Vue to send requests and set the proxy server URL.

Interpretation of Botanix: decentralized BTC L2 for network asset management (with interactive tutorial) Interpretation of Botanix: decentralized BTC L2 for network asset management (with interactive tutorial) May 08, 2024 pm 06:40 PM

Yesterday, BotanixLabs announced that it has completed a total of US$11.5 million in financing, with participation from Polychain Capital, Placeholder Capital and others. Financing will be used to build the decentralized EVM equivalent of BTCL2Botanix. Spiderchain combines the ease of use of EVM with the security of Bitcoin. Since the testnet went live in November 2023, there have been more than 200,000 active addresses. Odaily will analyze Botanix’s characteristic mechanism and testnet interaction process in this article. Botanix According to the official definition, Botanix is ​​a decentralized Turing-complete L2EVM built on Bitcoin and consists of two core components: Ethereum Virtual Machine

How to use explain in oracle How to use explain in oracle May 03, 2024 am 12:06 AM

The EXPLAIN command in Oracle is used to analyze the execution plan of a SQL statement. The method of use is to add the EXPLAIN keyword before the SQL statement. EXPLAIN results contain information such as ID, operator type, row count estimate, cost estimate, output row count estimate, access predicates, and filter predicates, which can be used to optimize query performance, identify costly operators, and tables that may benefit from optimization techniques.

See all articles