简体   繁体   English

Python oauth()访问twitter,“无模块oauth”

[英]Python oauth() to access twitter, “No Module oauth”

I am trying to test oauth to access twitter using code from a course. 我正在尝试测试oauth以使用课程中的代码访问Twitter。 I have a python script called twurl.py that holds this code (hidden holds the keys/tokens): 我有一个名为twurl.py的Python脚本,其中包含以下代码(隐藏的包含键/令牌):

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()

I am calling on the augment function in another script twtest.py: 我在另一个脚本twtest.py中调用增强函数:

import urllib
from twurl import augment 

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

I am running into this error: 我遇到这个错误:

Traceback (most recent call last):
 File "/Users/user/Desktop/python/twtest.py", line 7, in <module>
{'screen_name': 'user', 'count': '2'} )
File "/Users/user/Desktop/Python/twurl.py", line 6, in augment
secrets = hidden.oauth()
AttributeError: 'module' object has no attribute 'oauth'

This code works in an online lecture I am using to learn. 该代码可在我正在学习的在线讲座中使用。 Oauth was pip installed by me and I can call the dir(oauth). Oauth是我安装的pip,我可以叫dir(oauth)。

What am I missing, why is oauth not being imported along with the twurl script? 我缺少什么,为什么不将auth和twurl脚本一起导入? Even if I import oauth in twtest.py the same error occurs. 即使我在twtest.py中导入oauth,也会发生相同的错误。 Thanks! 谢谢!

EDIT: 编辑:

Hidden module is a list of called variables: 隐藏模块是一个称为变量的列表:

 consumer_key = 'string'
 consumer_secret = 'string'
 token_key = 'string'
 token_secret = 'string'

I just came through the same problem (referring to hte twurl.py script from the Dr Chuck courses on python). 我只是遇到了同样的问题(请参阅python的Dr Chuck课程中的hte twurl.py脚本)。

Everything worked fine when I put all the python scripts in the same folder as twurl.py (ad particularly oauth.py). 当我将所有python脚本与twurl.py(尤其是oauth.py)放在同一文件夹中时,一切工作正常。

My understanding is that the twurl.py script required another script (oauth.py), and that installing new libraries (like oauth through pip installer) was not doing any changes. 我的理解是twurl.py脚本需要另一个脚本(oauth.py),并且安装新库(例如通过pip安装程序安装oauth)没有做任何更改。

I had the same problem. 我有同样的问题。 I find the solution in the book *Python for Everybody page 167. 我在《 Python for Everybody》第167页中找到了解决方案。

For this next sample program we will download the files twurl.py, hidden.py, oauth.py, and twitter1.py from www.py4e.com/code and put them all in a folder on your computer. 对于下一个示例程序,我们将从www.py4e.com/code下载twurl.py,hidden.py,oauth.py和twitter1.py 文件 ,并将它们全部放入计算机的文件夹中。

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

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