Can you guys help me out? keep getting unauthorized every time

I’m a novice coder and I’m using ChatGPT to to help me build this script. Can ya’ll tell me what’s wrong?

import http.client
import hmac
import hashlib
import json
import time
import base64

# Set API key and secret key
ACCESS_KEY = ""
SECRET_KEY = ""

# Set URL and request path
conn = http.client.HTTPSConnection("api.coinbase.com")
path = "/api/v3/brokerage/accounts"
method = "GET"
body = ''

# Generate timestamp
timestamp = str(int(time.time()))
message = timestamp + method + path + body
signature = hmac.new(SECRET_KEY.encode('utf-8'), message.encode('utf-8'), digestmod=hashlib.sha256).hexdigest()

# Set headers
headers = {
"accept": "application/json",
"CB-ACCESS-KEY": ACCESS_KEY,
"CB-ACCESS-SIGN": signature,
"CB-ACCESS-TIMESTAMP": timestamp
}

# Send request
conn.request(method, path, body, headers)

# Get response
res = conn.getresponse()
data = res.read()

# Print response
print(data.decode("utf-8"))

Hi @daghz! Welcome to the Coinbase Cloud Developer’s Forum!

We understand that you are having trouble with regards to authorization. Based on the code snippet you have provided, we noticed that you are actually using SECRET_KEY instead of ACCESS_KEY for generating the signature. You may find relevant information in our documentation about Authenticating Messages, such as sample Signature Generation codes which you may refer to and check if the proper syntax was followed. In addition, please note that we also found a similar forum post with your case scenario and we suggest that you also try looking into it for reference.

We hope this helps. Thank you and have a great day!

1 Like

I would probably not recommend using ChatGPT to generate code for the Advanced Trade API. Not that it won’t work or I hate it or anything, it’s just that ChatGPT was trained on data that was available before Advanced Trade was release to the public in beta, so from my experience at least, it will give you a lot of code snippets that work on the Pro/Exchange API which don’t work the same way with Advanced Trade. It’s a good way to get confused, at least until they update the language model.

2 Likes

Hey! actually the code works, and it was completely generated by ChatGPT.

My API keys were overused? I created a new set and they work. You can train ChatGPT :wink:

I don’t know why I’m not able to edit the OP but here’s my updated code for anyone struggling

import http.client
import hmac
import hashlib
import json
import time
import base64
    
# Set API key and secret key
accessKey = ""
secretKey = ""
    
# Set URL and request path/api/v3/brokerage/accounts/"
conn = http.client.HTTPSConnection("api.coinbase.com")
path = "/api/v3/brokerage/accounts?limit=250"
method = "GET"
body = ''

# Generate timestamp
timestamp = str(int(time.time()))
message = timestamp + method + path.split('?')[0] + str(body)
signature = hmac.new(secretKey.encode('utf-8'), message.encode('utf-8'), digestmod=hashlib.sha256).hexdigest()

# Set headers
headers = {
"accept": "application/json",
"CB-ACCESS-KEY": accessKey,
"CB-ACCESS-SIGN": signature,
"CB-ACCESS-TIMESTAMP": timestamp
}

# Send request
conn.request(method, path, body, headers)

# Get response
res = conn.getresponse()
data = res.read()

# Parse response data as JSON
response_data = json.loads(data.decode("utf-8"))

# Print response data in a more readable format
print(json.dumps(response_data, indent=2))
1 Like