import constants
import oauth2
import urllib.parse as urlparse
import json
#Create a consumer, which uses CONSUMER_KEY and CONSUMER_SECRET to identify our app uniquely
consumer = oauth2.Consumer(constants.CONSUMER_KEY, constants.CONSUMER_SECRET)
client = oauth2.Client(consumer)
#Use the client to perform a request for the request token
response, content = client.request(constants.REQUEST_TOKEN_URL, 'POST')
if response.status != 200:
print("An error occurred getting the token from Twitter!")
#Get the request token parsing the query string returned
request_token = dict(urlparse.parse_qsl(content.decode('utf-8')))
#Ask the user to authorize our app and give us the pin code
print("Go to the follwing site in your brower:")
print("{}?oauth_token={}".format(constants.AUTHORIZATION_URL, request_token['oauth_token']))
oauth_verifier = input("What is the PIN? ")
#Create a Token object which contains the request toen, and the verifier
token = oauth2.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
token.set_verifier(oauth_verifier)
#Create a client with our consumer (our app) and the newly created (and verified) token
client = oauth2.Client(consumer, token)
#Ask Twitter for an acess token, and Twitter knows ot should give us it because we've verified the request token
response, content = client.request(constants.ACCESS_TOKEN_URL, 'POST')
access_token = dict(urlparse.parse_qs(content.decode('utf-8')))
print(access_token)
#Create an "authorized_token" Token object and use that to perfom Twitter API calls on behalf of user
authorized_token = oauth2.Token(access_token['oauth_token'], access_token['oauth_token_secret'])
authorized_client = oauth2.Client(consumer, authorized_token)
#Make Twittwe API calls!
response, content = authorized_client.request('https://api.twitter.com/1.1/search/tweets.json?q=computers+filter:images', 'GET')
if response.status != 200:
print("An error occurred when searching!")
print(content.decode('utf-8'))
我收到以下错误消息-AttributeError'list'对象没有属性'encode'。 有人可以向我解释为什么我收到此错误,我的代码中没有看到任何错误。
我的完整错误是以下代码:
Exception has occurred: AttributeError
'list' object has no attribute 'encode'
File "/home/bruno/Documents/Python/project/login.py", line 42, in <module>
response, content = authorized_client.request('https://api.twitter.com/1.1/search/tweets.json?q=computers+filter:images', 'GET')
File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/usr/lib/python3.6/runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "/usr/lib/python3.6/runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
答案 0 :(得分:1)
假设您正在尝试将python2代码转换为python3,则python 3受编码和解码功能的支持,它始终提供utf-8兼容的字符串,始终不用担心编码和解码会删除它们,因此您将摆脱该错误。 如果您想使用编码或解码,请参阅此url