Python Django successfully calls the Sina Weibo API (latest ).

Source: Internet
Author: User

Download the python Weibo SDK from the address below

Http://michaelliao.github.com/sinaweibopy/

Download the installation file and install it using Python setup. py install.

#-*-Coding: UTF-8-*-import sysimport weiboimport webbrowserapp_key = '2xxxx1 '# app keyapp_secret = 'authorization' # app secret = app_secretredirect_url = 'HTTP: // 127.0.0.1: 8000/market/Weibo '# A: this is an important API = Weibo. apiclient (app_key, my_app_secret) authorize_url = API. get_authorize_url (redirect_url) print (authorize_url) webbrowser. open_new (authorize_url) code = raw_input () # B: This is very important request = API. request_access_token (Code, redirect_url) access_token = request. access_tokenexpires_in = request. expires_inapi.set_access_token (access_token, expires_in) # print (API. statuses _ public_timeline () print 'user _ timeline' r = API. statuses. user_timeline.get (uid = 1800946163) for St in R. statuses: Print St. text print 'friendships _ DS 'Friends = API. friendships. friends. get (uid = 1800946163, Count = 200) print friends

Then, save the above Code to testweibo. py. The main points are as follows:

1, app_key and app_secret You Need To Go To The http://open.weibo.com to apply for a website or app to find

2. A: I have read the callback_url In the document. You can write it as a local address. After all, you need to obtain an authentication code when running Django on your machine;Note that you must configure the authorization callback page (callbackurl). Otherwise, even if you write a real callbackurl in the code, an error is returned, it does not matter whether your app is approved or not. I think many people are stuck in this step.

3. B: after waiting for webbrowser and open_new authentication page, you will get an authentication code and enter it manually.

 

So what exactly is my redirect_url = 'HTTP: // 127.0.0.1: 8000/market/Weibo '?

1. Actually, I wrote a view to print out the code.

@ Staticmethod def weibotest (request): print request. Get return httpresponse ("generated successfully! ")

:

When you call back your page after Weibo authentication, the code will be taken over so that you can see it.

What should I do next?

Of course, go back to the command line that executes testweibo. py and enter the code and press enter, as shown in the figure:

This is the result of obtaining friends.

For API calls, you can try to use a tool to test together:

You will naturally think of the naming rules for API calls:

URL: https://api.weibo.com/2/statuses/public_timeline.json
Next, make a change to the API name: [Replace the backslash/with a double underscore __]
The following figure shows statuses _ public_timeline.
Then, statuses _ public_timeline () is the member function name of apiclient.

Extended thinking:

For projects on Django, the method of getting code can be saved to the session first, and then transferred for use. Otherwise, it is not very tiring to copy and paste each time you open an Authentication Window.

For projects on python, you can use the File Sharing Method to save the code to the file and then call and use it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.