2016-08-30 79 views
1

我創建了一個腳本,將獲得用戶朋友列表(GET請求),我成功了。現在我正在嘗試製作一個跟隨特定用戶(POST請求)的腳本,但我一直不成功。oauth2 POST - 推特

這裏是我的OAuth功能(問題所在):

def augment_POST(url,**kwargs) : 
    secrets = hidden.oauth() 
    consumer = oauth2.Consumer(secrets['consumer_key'], secrets['consumer_secret']) 
    token = oauth2.Token(secrets['token_key'],secrets['token_secret']) 

    oauth_request = oauth2.Request.from_consumer_and_token(consumer, token= token, http_method='POST', http_url=url, parameters=kwargs) 
    oauth_request.to_postdata() # this returns post data, where should i put it? 
    oauth_request.sign_request(oauth2.SignatureMethod_HMAC_SHA1(), consumer, token) 
    return oauth_request.to_url() 

我augment_GET功能,除了http_mehtod = 'GET'

爲清楚起見,同樣的事情:

def follow_user(id): 
    seedurl="https://api.twitter.com/1.1/friendships/create.json" 
    print 'Attempting to follow: %d' % (id,) 
    url = augment_POST(seedurl,user_id=id) 
    connection = urllib.urlopen(url) 
    data = connection.read() 
    headers = connection.info().dict 

任何幫助將不勝感激。

+0

它不應該使用oauth2來提出此請​​求。這個OAuth庫是什麼? – odrling

+0

沒關係這個庫有一個令人困惑的名字 – odrling

+0

我得到一個錯誤404與您的鏈接 – odrling

回答

1

首先,您似乎需要import urllib2來發出POST請求。
你要發送您在使用data說法urlopento_postdata方法 得到POST數據:

def augment_POST(url, **kwargs) : 
    secrets = hidden.oauth() 
    consumer = oauth2.Consumer(secrets['consumer_key'], 
           secrets['consumer_secret']) 
    token = oauth2.Token(secrets['token_key'], 
         secrets['token_secret']) 

    oauth_request = oauth2.Request.from_consumer_and_token(
     consumer, 
     token= token, 
     http_method='POST', 
     http_url=url, 
     parameters=kwargs 
    ) 

    oauth_request.sign_request(oauth2.SignatureMethod_HMAC_SHA1(), 
           consumer, token) 

    # this is the data that must be sent with you POST request 
    return oauth_request.to_postdata() 


def follow_user(id): 
    url = "https://api.twitter.com/1.1/friendships/create.json" 
    print 'Attempting to follow: %d' % id 
    postdata = augment(url, method='GET', user_id=id) 

    # Send the POST request with the data argument 
    # The url is the same as the data is sent in the body of the request 
    connection = urllib2.urlopen(url, data=postdata) 

    data = connection.read() 
    headers = connection.info().dict 

我會建議使用requests_oauthlib模塊,這使得這一切都非常簡單:

from requests_oauthlib import OAuth1Session 

tokens = hidden.oauth() 
client = OAuth1Session(tokens['consumer_key'], 
         tokens['consumer_secret'], 
         tokens['token_key'], 
         tokens['token_secret']) 


def follow_user(id): 
    url = "https://api.twitter.com/1.1/friendships/create.json" 
    print 'Attempting to follow: %d' % id 

    # for GET requests use client.get and the `params` argument 
    # instead of the `data` argument 
    response = client.post(url, data={'user_id': id}) 

    data = response.text 
    # or even `data = response.json()` to decode the data 
    headers = response.headers 
+0

非常感謝你!我可以和另一個話題聯繫嗎?從Twitter獲取不記名令牌。我站在這個概念,我只是不知道你如何實現它。 – MintDrop

+0

要獲得不記名令牌,必須使用消費者密鑰作爲用戶名和消費者密碼作爲密碼來製作基本授權標頭。然後,您使用您生成的標題向https://api.twitter.com/oauth2/token發送POST請求。您可以使用json來解析響應並獲得持有者。然後,只需將您的不記名令牌放在您的驗證令牌中,格式爲「承載者{bearer_token}」。 – odrling