Jump to content
Fórum Script Brasil
  • 0

Problemas de integração com API Twitter x Python


wesleyagenda

Question

Boa tarde galera!,

Estou com problemas de integração da API do Twitter com minha aplicação do Python. Abaixo encaminho meu código e o error.

Código:

import urllib.request
import time
import json
import socket
import ssl
def send_to_twitter():
msg = ("I am a message that will be sent to twitter")
password_manager = urllib.request.HTTPPasswordMgr()
password_manager.add_password("Twitter API","http://twitter.com/statuses","usuario","senha")
http_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
page_opener = urllib.request.build_opener(http_handler)
urllib.request.install_opener(page_opener)
params = urllib.parse.urlencode({'status':msg})
resp = urllib.request.urlopen("http://twitter.com/statuses/update.json",params)
resp.read()
def get_price():
page = urllib.request.urlopen("http://beans-r-us.appspot.com/prices.html")
text = page.read().decode("utf8")
where = text.find('>$')
start_of_price = where + 2
end_of_price = start_of_price + 4
return float(text[start_of_price:end_of_price])
price_now = input("Do you want to see the price now (y/n)?")
if price_now == "y":
#print (get_price())
send_to_twitter()
else:
price = 99.99
while price >4.74:
time.sleep(900)
price = get_price()
#print ("BUY!")
send_to_twitter()

Error:

Do you want to see the price now (y/n)?y
Traceback (most recent call last):
File "C:\Python34\Exercicio\BUY.py", line 29, in <module>
send_to_twitter()
File "C:\Python34\Exercicio\BUY.py", line 16, in send_to_twitter
resp = urllib.request.urlopen("http://twitter.com/statuses/update.json",params)
File "C:\Python34\lib\urllib\request.py", line 161, in urlopen
return opener.open(url, data, timeout)
File "C:\Python34\lib\urllib\request.py", line 461, in open
req = meth(req)
File "C:\Python34\lib\urllib\request.py", line 1112, in do_request_
raise TypeError(msg)
TypeError: POST data should be bytes or an iterable of bytes. It cannot be of type str.
>>>
Link to comment
Share on other sites

1 answer to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...