简体   繁体   English

在python中使用Oauth库

[英]Using Oauth library in python

I am running a twitter api in python using oauth library. 我正在使用oauth库在python中运行Twitter API。 I have included the code below. 我已包含以下代码。 When I run the code "twtest.py", I get the error `'module' object has no attribute 'OAuthConsumer'. 当我运行代码“ twtest.py”时,出现错误“模块”对象没有属性“ OAuthConsumer”。

1.twtest.py 1.twtest.py

  import urllib
    from twurl import augment

    print '* Calling Twitter...'
    url = augment('https://api.twitter.com/1.1/statuses/user_timeline.json',
            {'screen_name': 'saurabhpathak20', 'count': '2'} )
    print url
    connection = urllib.urlopen(url)
    data = connection.read()
    print data
    headers = connection.info().dict
    print headers

2.twurl.py 2.twurl.py

import urllib
import oauth
import hidden

def augment(url, parameters) :
    secrets = hidden.oauth()
    consumer = oauth.OAuthConsumer(secrets['consumer_key'], secrets['consumer_secret'])
    token = oauth.OAuthToken(secrets['token_key'],secrets['token_secret'])

    oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, 
        token=token, http_method='GET', http_url=url, parameters=parameters)
    oauth_request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(), consumer, token)
    return oauth_request.to_url()


def test_me() :
    print '* Calling Twitter...'
    url = augment('https://api.twitter.com/1.1/statuses/user_timeline.json',
        {'screen_name': 'saurabhpathak20', 'count': '2'} )
    print url
    connection = urllib.urlopen(url)
    data = connection.read()
    print data
    headers = connection.info().dict
    print headers

3.hidden.py 3隐藏

def oauth() :
    return { "consumer_key" : "pj......U8fFRyjV",
        "consumer_secret" : "zty3njhO4IRl........ELh1YC1j1rX",
        "token_key" : "515167047-xaRfSm7.......wBBOrjNd61anI55D",
        "token_secret" : "  y7ZCBDf6d..........x1eJV8mHRnL8hh" }

Kindly help me to understand what is wrong in the code. 请帮助我理解代码中的错误。 Thanks. 谢谢。

You need to import oauth from oauth instead of import oauth 您需要导入oauthoauth ,而不是import oauth

>>> from oauth import oauth
>>> oauth.OAuthConsumer
<class 'oauth.oauth.OAuthConsumer'>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM