Algosdk Forbidden Error - General - Algorand
Algosdk Forbidden Error
post by isha on Jun 9, 2023
I am trying to transfer Algos from one account to another using api.
This is the error I am getting
Amount from function 2420000
Inside Operations---------------- <function algotransfer at 0x7f67f1ab07b8>
27.97.133.234 - - [09/Jun/2023 10:22:42] "POST /useralgopayment HTTP/1.1" 500 -
Traceback (most recent call last):
File "/home/azureuser/.local/lib/python3.6/site-packages/algosdk/v2client/algod.py", line 78, in algod_request
resp = urlopen(req)
File "/usr/lib/python3.6/urllib/request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.6/urllib/request.py", line 532, in open
response = meth(req, response)
File "/usr/lib/python3.6/urllib/request.py", line 642, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python3.6/urllib/request.py", line 570, in error
return self._call_chain(*args)
File "/usr/lib/python3.6/urllib/request.py", line 504, in _call_chain
result = func(*args)
File "/usr/lib/python3.6/urllib/request.py", line 650, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/azureuser/.local/lib/python3.6/site-packages/flask/app.py", line 2091, in __call__
return self.wsgi_app(environ, start_response)
File "/home/azureuser/.local/lib/python3.6/site-packages/flask/app.py", line 2076, in wsgi_app
response = self.handle_exception(e)
File "/home/azureuser/.local/lib/python3.6/site-packages/flask_cors/extension.py", line 165, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/azureuser/.local/lib/python3.6/site-packages/flask/app.py", line 2073, in wsgi_app
response = self.full_dispatch_request()
File "/home/azureuser/.local/lib/python3.6/site-packages/flask/app.py", line 1518, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/azureuser/.local/lib/python3.6/site-packages/flask_cors/extension.py", line 165, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/azureuser/.local/lib/python3.6/site-packages/flask/app.py", line 1516, in full_dispatch_request
rv = self.dispatch_request()
File "/home/azureuser/.local/lib/python3.6/site-packages/flask/app.py", line 1502, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/home/azureuser/algorand/TraceChain/api/app.py", line 44, in useralgopayment
txn = algopayments.algo_transfer(sender,receiver,amount)
File "/home/azureuser/algorand/TraceChain/api/auction_demo/algopayments.py", line 28, in algo_transfer
amount = amount
File "/home/azureuser/algorand/TraceChain/api/auction_demo/auction/operations.py", line 28, in algotransfer
sp=client.suggested_params(),
File "/home/azureuser/.local/lib/python3.6/site-packages/algosdk/v2client/algod.py", line 337, in suggested_params
res = self.algod_request("GET", req, **kwargs)
File "/home/azureuser/.local/lib/python3.6/site-packages/algosdk/v2client/algod.py", line 85, in algod_request
raise error.AlgodHTTPError(e, code)
algosdk.error.AlgodHTTPError: Forbidden
I am using purestake token and url for the same.
There are 2 other apifor token transfer which are working being on same token and url.
Can anyone help me here?
algod_address = ""
algod_token = "xxx"
headers = {
"X-API-Key": algod_token,
}
algod_client = algod.AlgodClient(algod_token, algod_address,headers)
post by Tim on Jun 9, 2023
That is strange, typically you’d only see a 403 when the path is invalid, or the api-key is invalid.
Can you share the code that makes the call, you can mail me if you don’t want to post it.
post by isha on Jun 12, 2023
No problem I can share it here
def algo_transfer(sender,receiver,amount):
client = getAlgodClient()
sender = sender
print("Sender from function",sender)
receiver = receiver
print("Receiver from function",receiver)
amount = amount
print("Amount from function",amount)
txn = algotransfer(
client = client,
sender = sender,
receiver = receiver,
amount = amount
)
print("TestAlgo_transfer", txn)
print("FUNCTION----",algo_transfer)
return txn
def algotransfer(
client: AlgodClient,
sender: Account,
receiver: Account,
amount : int
):
print("Inside Operations----------------",algotransfer)
txn = transaction.PaymentTxn(
sender=sender,
receiver=receiver,
amt=amount,
sp=client.suggested_params(),
)
print("Inside Operations txn-----------------",txn)
txns = encoding.msgpack_encode(txn)
return txns
And the api used to call this is given below
@app.route('/useralgopayment', methods=['GET', 'POST'])
@cross_origin()
def useralgopayment():
request_data = request.get_json()
print("Inside UserALGO",request)
print("Inside UserALGO2",request_data)
sender = request_data['body']['Sender']
receiver = request_data['body']['Receiver']
amount = request_data['body']['Amount']
print("Inside UserALGO3---------",request_data)
print("Sender",sender)
print("Receiver",receiver)
print("Type of amount: ", type(amount))
txn = algopayments.algo_transfer(sender,receiver,amount)
print("Transaction------",txn)
return json.dumps({'status': 'Transaction Created','transaction' : txn})
post by fabrice on Jun 12, 2023
Can you show the algod_address you used?
post by isha on Jun 13, 2023
Its the algo_address from purestake testnet
https://testnet-algorand.api.purestake.io/ps2
post by fabrice on Jun 13, 2023
Can you try to do it using curl on the terminal:
curl -v -X GET "https://testnet-algorand.api.purestake.io/ps2/v2/transactions/params" -H "x-api-key:YourKey"
What do you see?
Can you compare you example with PureStake Developer Portal?
post by isha on Jun 14, 2023
Thanks @Tim and @fabrice it got resolved
There 2 files in my file structure on is the token-creation where all the functionalities of token are transfer option, freeze, unfreeze etc.
And the other one is setup.py where there are basic getGenesisAccounts KMD address and all is used.
So in that file I had just given this
algod_address = ""
algod_token = "xxx"
algod_client = algod.AlgodClient(algod_token, algod_address)
So I added headers there as well and it worked
algod_address = ""
algod_token = "xxx"
headers = {
"X-API-Key": algod_token,
}
algod_client = algod.AlgodClient(algod_token, algod_address,headers)