The Python script simultaneously releases Sina Weibo and twitter

Source: Internet
Author: User

The first step is to apply for a developer account first. For details, refer to the following section, which describes the authorization methods and principles, for example, if I am using a third-party SDK, I just took a simple look and did not have much in-depth understanding. However, if I want to develop the SDK, I can still study it well.

 

The following is the code:

 

From weibo import APIClient
From re import split
Import urllib, httplib

APP_KEY = '1 ****** 671 '# youre app key
APP_SECRET = 'e623c ************** bfa30b23' # youre app secret
# Callback url, your must set this URL in your "my application-> appInfos-> advanced info"
CALLBACK_URL = 'HTTP: // ww ***** shore.com'
ACCOUNT = 'bo ****** @ gmail.com '# your email address
PASSWORD = '**********' # your pw

# For getting the code contained in the callback url
Def get_code (url ):
Conn = httplib. HTTPSConnection ('api .weibo.com ')
Postdata = urllib. urlencode ({'client _ id': APP_KEY, 'response _ type': 'code', 'redirect _ uri ': CALLBACK_URL, 'Action': 'submit ', 'userid': ACCOUNT, 'passwd': PASSWORD, 'isloginsina ': 0, 'from': '', 'regcallback':'', 'state ':'', 'ticket ': '', 'withofficalflag': 0 })
Conn. request ('post', '/oau2/ authorize', postdata, {'Referer': url, 'content-type ': 'application/x-www-form-urlencoded '})
Res = conn. getresponse ()
Location = res. getheader ('location ')
Code = location. split ('=') [1]
Conn. close ()
Return code

Def post_weibo (post_contents ):
Print "weibo posting ..."
# For getting the authorize url
Client = APIClient (app_key = APP_KEY, app_secret = APP_SECRET, redirect_uri = CALLBACK_URL)
Url = client. get_authorize_url ()
Code = get_code (url)
R = client. request_access_token (code)
Access_token = r. access_token # The token return by sina
Expires_in = r. expires_in
# Save the access token
Client. set_access_token (access_token, expires_in)
Results = client. post. statuses _ update (status = post_contents)
Return results


In the above Code, note that the get_code () function obtains the authorization code, which is not included in the original SDK, however, with this step missing, we need to manually click connection authorization on the webpage of the browser area, but I only want to do everything in the command line, so I have this function, in the function, the httplib packet is used to simulate the web request and process the returned information to obtain the authorization code.

 

The failure to implement Renren is also due to the issue of authorization code. You can refer to Renren's developer documentation. The authorization method is similar to that of Weibo. The difference is that we cannot send requests by code to obtain the authorization code, this explains why the python sdks recommended by Renren are all mounted on GAE. It should be for this reason that we need to grant permissions on the webpage in person, I gave up everyone. In fact, they have a user name and password service, but they need to fill out the form for application. I have filled in the form, but there is no message at the end. I believe if I have successfully applied, then you can obtain the authorization code in the same way as Weibo to obtain the tokens that calls the API. I still hope that the talented Daniel will succeed. I can read this article to learn from it.

 

Besides, Twitter


After reading the Twitter documents, we can see that the US emperor's things are good. They must use more advanced authorization methods, reduce authorization steps, and use REST APIs to make it easier for developers to develop sdks, so Twitter SDK is much better. The most popular tweepy is my first attempt. The cause of failure is not very clear, but it is related to the proxy I use, that is to say, if you use proxy upstreaming and Fb in China, you should note that using tweepy may cause failures. In addition, we also need to note that the requested twitter App has access level restrictions. The default value is read only. You can change it to read & write or read by yourself, both write and direct messages are supported.

 


Later, the bear python-twitter package I selected was particularly useful after it was installed, which made my code rarely changed and was also compatible with the network conditions of the proxy. The following code is used:

 

Import twitter
Def post_twitter (tweets ):
Print "tweets posting ..."
Tw_api = twitter. Api (consumer_key = '95otecq ************ xdqq ',
Consumer_secret = '1jueooha ********************** RducGal1iA ',
Access_token_key = '2017 ************************ LQZevkJD5spEi94 ',
Access_token_secret = '2qjrdux *********************** VRb7JBKaDGMtmI ')
Results = tw_api.PostUpdate (tweets)
Return results


From the code above, we can see that this SDK saves a lot of trouble, and there are no other shortcomings except that it makes the user look like there is no level.

 

The reason why I didn't get an Fb version is that the relationship between Fb and tw is really ambiguous. I bound the service that automatically synchronizes the status from Twitter to Fb, so when I update the Fb, I will keep up with it, and there is no need to do anything meaningless.


 

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.