示例#1
0
def get_access_token():
    auth = OAuthHandler(consumer_key=CONSUMER_KEY,consumer_secret=CONSUMER_SECRRET_KEY,callback_url=CALLBACK_URL)
    authorization_url = auth.get_authorization_url()
    request_tokens = auth.get_request_token()
    def get_atoken(env,res):
        if env['REQUEST_METHOD']=='GET':
            QUERY_STRING = env['QUERY_STRING']
            if QUERY_STRING:
                QUERY_STRING = parse_qs(QUERY_STRING)
                oauth_token = QUERY_STRING['oauth_token'][0]
                oauth_verifier = QUERY_STRING['oauth_verifier'][0]
                threading.Thread(target=httpd.shutdown).start()
                auth.set_request_token(request_tokens[0], request_tokens[1])
                access_tokens = auth.get_access_token(oauth_verifier)
                atoken_obj = {
                    'ACCESS_TOKEN':access_tokens[0],
                    'ACCESS_TOKEN_SECRET':access_tokens[1]
                }
                fp = file(PICKLE_FILENAME,'wb')
                pickle.dump(atoken_obj,fp)
                fp.close()
                res('200 OK',[('Content-type','text/html')])
                html = "<html><head><title>Authenticated Successfully!</title></head>"\
                       "<body>Authenticated Successfully!<br>"\
                       "Access Token: %s<br>"\
                       "Access Token Secret: %s<br></body></html>" % (access_tokens[0], access_tokens[1])
                return html
    try:
        if webbrowser.open(authorization_url) == False:
            raise webbrouwser.Error
    except webbrowser.Error as e:
        print(e)
        sys.exit(-1)
    except KeyboardInterrupt:
        sys.exit(1)

    host,port = urlparse(CALLBACK_URL).netloc.split(':')
    httpd = make_server(host,int(port),get_atoken)
    httpd.serve_forever()
示例#2
0
def update_text(your_blog_hostname, body):
    auth = OAuthHandler(CONSUMER_KEY,CONSUMER_SECRRET_KEY)
    auth.set_access_token(ACCESS_TOKEN,ACCESS_TOKEN_SECRET)
    api = API(auth)
    return api.update_text_post(your_blog_hostname,body)