Python Django successfully invokes Sina Weibo API (latest) with graphs

Source: Internet
Author: User
Download the Python microblogging SDK from the address below

http://michaelliao.github.com/sinaweibopy/


Download the installation file and install it yourself via Python setup.py install.


#-*-coding:utf-8-*-import

sys
import Weibo
import webbrowser

app_key = ' 2xxxx1 ' # APP KEY
app_sec RET = ' cb597a8xxxxxxxxxx71c3 ' # app secret


my_app_secret = app_secret
redirect_url = ' http://127.0.0.1:8000/ Market/weibo ' #A: This is 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 important.

request = Api.request_access_token (Code, Redirect_url)

Access_token = Request.access_token

expires_in = request.expires_in

api.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. R.statuses:
    print st.text
    
print ' friendships_friends '
friends= api.friendships.friends.get (uid=1800946163,count=200)
print Friends
Then save the above code to testweibo.py, here are a few things to say:

1, App_key and App_secret need you to http://open.weibo.com their own application of the website or APP to find

2, A: I read the document written on the Callback_url, this you can write a local address, after all, your own machine running Django, need to obtain a certification code; Here special note that you want to configure the authorization callback page (CALLBACKURL) in the following figure. Otherwise, even if you write a real callbackurl in the code, it will also be an error, this and your app is through the audit or not through the audit does not matter, I see a lot of people are stuck in this step.

3, B: Is the WEBBROWSER,OPEN_NEW certification page, you will get a certification code, their own manual input on the line



that my redirect_url = ' Http://127.0.0.1:8000/market/weibo ' In the end what is carried out, please look down

1, in fact, I wrote a view of the code to print out just


@staticmethod
    def weibotest (request):
        print request. Get Return
        HttpResponse ("Build succeeded.") ")

Screenshots:


After the Weibo authentication callback your page time, will bring the code to come over, this way can see.


how to proceed next.

Of course, back to the execution of the testweibo.py command line, the code input into the return, look at the picture:



This is the result of getting friends.


For API calls, you can try using tools to match tests:



You will naturally think of what the naming conventions for API calls are, hehe, as follows:


Url:https://api.weibo.com/2/statuses/public_timeline.json
Second, make a transform to the API name: "Turn the backslash/replace to double underline __"
Get here: Statuses__public_timeline
So, Statuses__public_timeline () is the name of the Apiclient member function.



Expand your thinking:

For Django on the project, get the code can be stored in the session, and then mobilize the use, or each time to open a certification window copy sticky paste is not very tired.

For projects on Python, you can use file sharing to save code in a file and then invoke it.



Related Article

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.