Python3 - coinbase API authentication - python-3.6

I am trying to access my coinbase account using their API.
I am using the following code: (which is from the website https://developers.coinbase.com/docs/wallet/api-key-authentication)
import json, hmac, hashlib, time, requests
from requests.auth import AuthBase
# Before implementation, set environmental variables with the names API_KEY and API_SECRET
API_KEY = 'API_KEY'
API_SECRET = 'API_SECRET'
# Create custom authentication for Coinbase API
class CoinbaseWalletAuth(AuthBase):
def __init__(self, api_key, secret_key):
self.api_key = api_key
self.secret_key = secret_key
def __call__(self, request):
timestamp = str(int(time.time()))
message = timestamp + request.method + request.path_url + (request.body or '')
signature = hmac.new(self.secret_key, message, hashlib.sha256).hexdigest()
request.headers.update({
'CB-ACCESS-SIGN': signature,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': self.api_key,
})
return request
api_url = 'https://api.coinbase.com/v2/'
auth = CoinbaseWalletAuth(API_KEY, API_SECRET)
# Get current user
r = requests.get(api_url + 'user', auth=auth)
print r.json()
# {u'data': {u'username': None, u'resource': u'user', u'name': u'User'...
However I am getting the following error:
Traceback (most recent call last):
File "test1.py", line 44, in <module>
r = requests.get(api_url + 'user', auth=auth)
File "C:\Users\lclar\virtualenv-env\lib\site-packages\requests\api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\lclar\virtualenv-env\lib\site-packages\requests\api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\lclar\virtualenv-env\lib\site-packages\requests\sessions.py", line 494, in request
prep = self.prepare_request(req)
File "C:\Users\lclar\virtualenv-env\lib\site-packages\requests\sessions.py", line 437, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "C:\Users\lclar\virtualenv-env\lib\site-packages\requests\models.py", line 309, in prepare
self.prepare_auth(auth, url)
File "C:\Users\lclar\virtualenv-env\lib\site-packages\requests\models.py", line 540, in prepare_auth
r = auth(self)
File "test1.py", line 29, in __call__
signature = hmac.new(self.secret_key, message, hashlib.sha256).encode("utf-8").digest()
File "C:\Users\lclar\AppData\Local\Programs\Python\Python36-32\lib\hmac.py", line 144, in new
return HMAC(key, msg, digestmod)
File "C:\Users\lclar\AppData\Local\Programs\Python\Python36-32\lib\hmac.py", line 42, in __init__
raise TypeError("key: expected bytes or bytearray, but got %r" % type(key).__name__)
TypeError: key: expected bytes or bytearray, but got 'str'
Can someone please help me?
Thanks in advance.

I assuming the data type self.secret_key = secret_key is a string. For Python >= 3.4, hmac.new(key, msg=None, digestmod=''), the key must be of type bytes or bytearray per the docs: https://docs.python.org/3/library/hmac.html
Likewise to avoid the TypeError: Unicode-objects must be encoded before hashing error, do the same for the message variable as shown below:
signature = hmac.new(self.secret_key.encode(), message.encode(), hashlib.sha256).hexdigest()

Related

How to write this HTTP request in python?

I need to write this HTTP request.
I was able to get the token (from another HTTP request) and I saved on in a variable name "Token"
Can you please help me write this HTTP request in python?
I am getting 404 and I am pretty sure the syntax is wrong.
Attaching a screenshot from Microsoft documentation.
the new error message:
Traceback (most recent call last):
File "/Users/talcohen/opt/anaconda3/lib/python3.9/site-packages/requests/models.py", line 972, in json
return complexjson.loads(self.text, **kwargs)
File "/Users/talcohen/opt/anaconda3/lib/python3.9/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/Users/talcohen/opt/anaconda3/lib/python3.9/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Users/talcohen/opt/anaconda3/lib/python3.9/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/talcohen/Documents/ddd/PowerBI_REST_API/CreateProfile.py", line 15, in <module>
data = requests.post(api_url, json=payload, headers=headers).json()
File "/Users/talcohen/opt/anaconda3/lib/python3.9/site-packages/requests/models.py", line 976, in json
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
(base) talcohen#Tals-Mac-mini PowerBI_REST_API %
Looking at the API you should probably do:
import requests
token = "<YOUR TOKEN>"
api_url = "https://api.powerbi.com/v1.0/myorg/profiles"
headers = {"Authorization": f"Bearer {token}"}
payload = {"displayName": "My First Profile"}
data = requests.post(api_url, json=payload, headers=headers).json()
print(data)

Strava GPX upload with python module requests is giving a ValueError - why?

I don't understand why this ValueError occurs?
Post request
import requests
headers = {'authorization': 'Bearer XXXXXXXXXXXXXXXX'}
files = [{'file': open('../poc_data/Sport-sessions/GPS-data/2012-08-09_05-32-43-UTC_55d566fa93bc7d9d1757b8e0.gpx', 'rb')}, {'data_type': 'gpx'}, {'Content-Type': 'multipart/form-data'}]
r = requests.post('https://www.strava.com/api/v3/uploads', headers=headers, files=files)
Error traceback
*Traceback (most recent call last):
File "D:/projekte/11_github/poc_runtastic/poc_code/strava_upload_activities.py", line 19, in <module>
r = requests.post('https://www.strava.com/api/v3/uploads', headers=headers, files=files)
File "E:\Python\Python38\lib\site-packages\requests\api.py", line 117, in post
return request('post', url, data=data, json=json, **kwargs)
File "E:\Python\Python38\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, *kwargs)
File "E:\Python\Python38\lib\site-packages\requests\sessions.py", line 528, in request
prep = self.prepare_request(req)
File "E:\Python\Python38\lib\site-packages\requests\sessions.py", line 456, in prepare_request
p.prepare(
File "E:\Python\Python38\lib\site-packages\requests\models.py", line 319, in prepare
self.prepare_body(data, files, json)
File "E:\Python\Python38\lib\site-packages\requests\models.py", line 512, in prepare_body
(body, content_type) = self._encode_files(files, data)
File "E:\Python\Python38\lib\site-packages\requests\models.py", line 141, in
_encode_files
for (k, v) in files:
ValueError: not enough values to unpack (expected 2, got 1)
The reason for that effect regards to the API-design: it is expecting one explicit dictionary named headers and one named params.
In my given example above I sent only one (headers) and that explains the ValueError.
This code works:
import requests
files = {
'file': open('../poc_data/Sport-sessions/GPS-data/2012-08-09_05-32-43-UTC_55d566fa93bc7d9d1757b8e0.gpx', 'rb')
}
headers = {
'authorization': 'Bearer XXXXXXXXXXXXXXXX'
}
params = {
'data_type': 'gpx'
}
url = 'https://www.strava.com/api/v3/uploads'
r = requests.post(files=files, headers=headers, params=params, url=url)

async with" if the event loop is running

I'm writing my first telegram bot with telepot and telethon
my main code is:
import sys
import asyncio
import random
import telepot
import telepot.aio
from telepot.aio.loop import MessageLoop
from telepot.namedtuple import ReplyKeyboardMarkup, KeyboardButton, ReplyKeyboardRemove, ForceReply
from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton
from telepot.namedtuple import InlineQueryResultArticle, InlineQueryResultPhoto, InputTextMessageContent
async def on_chat_message(msg):
global listenFromKeyboardUsername, listenFromKeyboardPassword, listenFromKeyboardLinkGroup
content_type, chat_type, chat_id = telepot.glance(msg)
chat_id = str(chat_id)
if content_type == 'text':
name = msg["from"]["first_name"]
txt = msg['text']
# stuff..
elif userExistsInDb and userData['listenFromKeyboardLinkGroup'] and chat_id == doc.id:
group = telegramGetMessages.checkGroup(txt)
print(group)
TOKEN = "*******"
bot = telepot.aio.Bot(TOKEN)
answerer = telepot.aio.helper.Answerer(bot)
loop = asyncio.get_event_loop()
loop.create_task(MessageLoop(bot, {'chat': on_chat_message,
'callback_query': on_callback_query}).run_forever())
print('Listening ...')
loop.run_forever()
from the code above I call the checkGroup function:
def checkGroup(hash):
initClient()
global result
hash = hash.replace('https://t.me/joinchat/', '')
with TelegramClient(name, api_id, api_hash) as client:
result = client(functions.messages.CheckChatInviteRequest(hash=hash))
if isinstance(result, ChatInvite):
print('You are not inside the group')
with TelegramClient(name, api_id, api_hash) as client:
client(functions.messages.ImportChatInviteRequest(hash=hash))
result = client(functions.messages.CheckChatInviteRequest(hash=hash))
return result
where I'm getting this error:
RuntimeError: You must use "async with" if the event loop is running (i.e. you are inside an "async def")
then I edit the checkGroup function with async def checkGroup(hash): ..
but now I'm geting this error and I don't know what to do:
Task exception was never retrieved
future: <Task finished coro=<Router.route() done, defined at /home/ale/PycharmProjects/newTelegramBot/venv/lib/python3.6/site-packages/telepot/aio/helper.py:213> exception=NotFound('No document to update: projects/telegram-bot-4ee9f/databases/(default)/documents/users/585089661/data/groups',)>
Traceback (most recent call last):
File "/home/ale/PycharmProjects/newTelegramBot/venv/lib/python3.6/site-packages/google/api_core/grpc_helpers.py", line 57, in error_remapped_callable
return callable_(*args, **kwargs)
File "/home/ale/PycharmProjects/newTelegramBot/venv/lib/python3.6/site-packages/grpc/_channel.py", line 549, in __call__
return _end_unary_response_blocking(state, call, False, None)
File "/home/ale/PycharmProjects/newTelegramBot/venv/lib/python3.6/site-packages/grpc/_channel.py", line 466, in _end_unary_response_blocking
raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with:
status = StatusCode.NOT_FOUND
details = "No document to update: projects/telegram-bot-4ee9f/databases/(default)/documents/users/585089661/data/groups"
debug_error_string = "{"created":"#1552727386.760400590","description":"Error received from peer","file":"src/core/lib/surface/call.cc","file_line":1039,"grpc_message":"No document to update: projects/telegram-bot-4ee9f/databases/(default)/documents/users/585089661/data/groups","grpc_status":5}"
>
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/ale/PycharmProjects/newTelegramBot/venv/lib/python3.6/site-packages/telepot/aio/helper.py", line 244, in route
return await _invoke(fn, msg, *args, **kwargs)
File "/home/ale/PycharmProjects/newTelegramBot/venv/lib/python3.6/site-packages/telepot/aio/helper.py", line 16, in _invoke
return await fn(*args, **kwargs)
File "/home/ale/PycharmProjects/newTelegramBot/chatAsync.py", line 119, in on_chat_message
database.updateUserData(chat_id, 'groups', 'nameGroup', txt)
File "/home/ale/PycharmProjects/newTelegramBot/database.py", line 38, in updateUserData
field: key,
File "/home/ale/PycharmProjects/newTelegramBot/venv/lib/python3.6/site-packages/google/cloud/firestore_v1beta1/document.py", line 371, in update
write_results = batch.commit()
File "/home/ale/PycharmProjects/newTelegramBot/venv/lib/python3.6/site-packages/google/cloud/firestore_v1beta1/batch.py", line 148, in commit
metadata=self._client._rpc_metadata,
File "/home/ale/PycharmProjects/newTelegramBot/venv/lib/python3.6/site-packages/google/cloud/firestore_v1beta1/gapic/firestore_client.py", line 946, in commit
request, retry=retry, timeout=timeout, metadata=metadata
File "/home/ale/PycharmProjects/newTelegramBot/venv/lib/python3.6/site-packages/google/api_core/gapic_v1/method.py", line 143, in __call__
return wrapped_func(*args, **kwargs)
File "/home/ale/PycharmProjects/newTelegramBot/venv/lib/python3.6/site-packages/google/api_core/retry.py", line 270, in retry_wrapped_func
on_error=on_error,
File "/home/ale/PycharmProjects/newTelegramBot/venv/lib/python3.6/site-packages/google/api_core/retry.py", line 179, in retry_target
return target()
File "/home/ale/PycharmProjects/newTelegramBot/venv/lib/python3.6/site-packages/google/api_core/timeout.py", line 214, in func_with_timeout
return func(*args, **kwargs)
File "/home/ale/PycharmProjects/newTelegramBot/venv/lib/python3.6/site-packages/google/api_core/grpc_helpers.py", line 59, in error_remapped_callable
six.raise_from(exceptions.from_grpc_error(exc), exc)
File "<string>", line 3, in raise_from
google.api_core.exceptions.NotFound: 404 No document to update: projects/telegram-bot-4ee9f/databases/(default)/documents/users/585089661/data/groups
/usr/lib/python3.6/asyncio/base_events.py:1441: RuntimeWarning: coroutine 'checkGroup' was never awaited
handle = None # Needed to break cycles when an exception occurs.
I'm reading around, and maybe for this problem it's better to use the delegatoBot, but I'm not really sure because I can't find great examples!
It wold be wonderfull recive a reply, If you need anything just ask!
Thankyou

telegram bot keeps replying rather than executing the following code (webhook is used)

I am trying to host my telegram bot for a multiplayer game on GAE and web hook is used here. This is how my databasing part is like:
class Game(ndb.Model):
chat_id = ndb.IntegerProperty(required = True)
mission_num = ndb.IntegerProperty(default =1)
round_num = ndb.IntegerProperty(default =1)
class Player(ndb.Model):
user_id = ndb.IntegerProperty(required=True)
player_role = ndb.StringProperty (
choices = ['spy','resistance'])
The part of code under web hook handler:
if text.startswith('/'):
if text == '/start':
reply('Bot enabled')
setEnabled(chat_id, True)
elif text == '/stop':
reply('Bot disabled')
setEnabled(chat_id, False)
elif text == '/newgame':
if chat_type == 'group':
existing_game = Game.query (Game.chat_id == chat_id).get()
if existing_game:
reply ("game is alr intitiated liao")
else:
##create a new game here
#still stuck here
##========================##
#reply("keep replying this line")
##========================##
new_game = Game (
chat_id = chat_id,
id = chat_id
)
curr_game_key = new_game.put()
new_player = Player (
parent = curr_game_key,
user_id = fr_user_id,
id = fr_user_id)
new_player.put()
reply("waiting for more friends to join")
else:
reply('game must be conducted within a group chat! jio more friends!')
else:
reply('What command?')
else:
if getEnabled(chat_id):
reply('I got your message! (but I do not know how to answer)')
else:
logging.info('not enabled for chat_id {}'.format(chat_id))
The problem is that when I send '/newgame' in a group chat, nothing is sent back to me. If I uncomment the following line my bot keeps sending me "keep replying this line" like crazy.:
#reply("keep replying this line")
The reply function:
def reply(msg=None, img=None):
if msg:
resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
'chat_id': str(chat_id),
'text': msg.encode('utf-8'),
'disable_web_page_preview': 'true',
'reply_to_message_id': str(message_id),
})).read()
elif img:
resp = multipart.post_multipart(BASE_URL + 'sendPhoto', [
('chat_id', str(chat_id)),
('reply_to_message_id', str(message_id)),
], [
('photo', 'image.jpg', img),
])
else:
logging.error('no msg or img specified')
resp = None
logging.info('send response:')
logging.info(resp)
The error:
Internal Server Error
The server has either erred or is incapable of performing the requested operation.
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~orbitaltest2/1.393734187774164753/main.py", line 66, in get
self.response.write(json.dumps(json.load(urllib2.urlopen(BASE_URL + 'getUpdates'))))
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 410, in open
response = meth(req, response)
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 448, in error
return self._call_chain(*args)
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 531, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 409: Conflict
code for the handler that line 66 belongs to:
class GetUpdatesHandler(webapp2.RequestHandler):
def get(self):
urlfetch.set_default_fetch_deadline(60)
self.response.write(json.dumps(json.load(urllib2.urlopen(BASE_URL + 'getUpdates'))))
Totally newbie, any suggestion is appreciated!
You should check what status code & what content is returned by your webhook.
You have 2 options how to reply to use:
Call Telegram API
Return JSON as a response to webhook call
As you have not provided it source code of reply() it's hard to tell what exactly is wrong.
Anyway your webhook should return HTTP status code 200. If it does not Telegram treat it as an internal error and is trying to resent message to you. This is why you are getting repeated calls and it's "replying like crazy".
Most probably the call reply("keep replying this line") is successful but then something wrong and Telegram gets wrong reply.
Add try/except blocks and log exceptions.
Check your logs & put additional logging if needed. For example I'm logging HTTP response content from my webhook. It helps.

Google Maps API - Sign Geocode Requests with Python 3

I'm trying to create a script to generate signed urls using my Maps API license to geocode addresses, but I'm having trouble with my sign_url function.
import urllib
import urllib.parse
import base64
import hashlib
import hmac
import re
google_apis_url_http = 'http://maps.googleapis.com'
geocoding_endpoint_json = '/maps/api/geocode/json'
def encode_params(address, client, sensor):
return urllib.parse.urlencode({'address': address, 'client': client, 'sensor': sensor})
def sign_url(url, private_key):
decoded_key = base64.urlsafe_b64decode(private_key)
signature = hmac.new(decoded_key, url, hashlib.sha1)
encoded_signature = base64.urlsafe_b64encode(signature.digest())
return encoded_signature
with open("addresses.txt", "r") as inputFile:
inputList = inputFile.read().splitlines()
for line_address in inputList[0:1]:
comma_stripped_address = re.sub(",", "", line_address)
formatted_address = re.sub(" ", "+", comma_stripped_address)
encoded_params = encode_params(formatted_address, 'gme-********', 'false')
url_to_sign = geocoding_endpoint_json + '?' + encoded_params
print(sign_url(url_to_sign, '********'))
Here's the error I'm getting:
Traceback (most recent call last):
File "/Users/[me]/PycharmProjects/Google APIs/geocode_auth.py", line 31, in <module>
print(sign_url(url_to_sign, '********'))
File "/Users/[me]/PycharmProjects/Google APIs/geocode_auth.py", line 18, in sign_url
signature = hmac.new(decoded_key, url, hashlib.sha1)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/hmac.py", line 131, in new
return HMAC(key, msg, digestmod)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/hmac.py", line 73, in __init__
self.update(msg)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/hmac.py", line 79, in update
raise TypeError("expected bytes, but got %r" % type(msg).__name__)
TypeError: expected bytes, but got 'str'
Anyone know why this TypeError is being raised? I'm modeling the script off of a demo done in Python v2, so it's possible something broke in v3...

Resources