Have a issue with request routed53omains API - python

There was a problem accessing the routedomains module API.
code:
#!/usr/bin/python3.6
#encoding: utf-8
import boto3
key_id='xxxxxxxx'
access_key='xxxxxxxx'
client = boto3.client(
'route53domains',
aws_access_key_id = key_id,
aws_secret_access_key = access_key,
region_name = 'us-east-1'
)
all_domains= client.list_domains(DomainName="mydomain.com")
print(all_domains)
Run result:
[]
I can confirm that there are more than one domainame under the account(key_id and access_key).
and given sufficient permissions:
AmazonRoute53DomainsFullAccess
AmazonRoute53FullAccess
But response is null.
And request Route53 modules api is not problem:
client = boto3.client(
'route53',
region_name = 'us-east-1',
aws_access_key_id = "xxxxxxxxxxxx",
aws_secret_access_key = "xxxxxxxxxxxx"
)
client.get_paginator('list_hosted_zones')
My other account both Route53 and Route53domain request is work ok !
why? What's the problem? please help me, thanks!

The actual syntax for Route53domains client operation to list all the domains is:
response = client.list_domains(
Marker='string',
MaxItems=123
)
If the number of domains that are associated with the current AWS account is greater than the value that you specified for MaxItems , you can use Marker to return additional domains.
Refer to the boto3 documentation for Route53Domains

thank for your answer.
#!/usr/bin/python3.6
#encoding: utf-8
import boto3
key_id='xxxxxxxx'
access_key='xxxxxxxx'
client = boto3.client(
'route53domains',
aws_access_key_id = key_id,
aws_secret_access_key = access_key,
region_name = 'us-east-1'
)
all_domains= client.list_domains(MaxItems=123)
print(all_domains)
run result:
{'Domains': [], 'ResponseMetadata': {'RequestId': '2aa6e538-66e3-11e8-a89f-273ad6c882c8', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '2aa6e538-66e3-11e8-a89f-273ad6c882c8', 'content-type': 'application/x-amz-json-1.1', 'content-length': '14', 'date': 'Sun, 03 Jun 2018 04:04:09 GMT'}, 'RetryAttempts': 0}}
Domains is []
Sorry, I originally wanted to ask get_domain_detail() function:
Client.get_domain_detail(DomainName="mytest.com")
but found use that list_domains doesn't even have a domainname,
so,written error。
And I use the CMD API tool is still the same result:
./aws route53domains list-domains --max-items 123
{
"Domains": []
}

This may be an aws account or background issue. I don't know why.

Related

Sp-api - seller partner api python

I want to connect to the seller partner api in python using boto3.
The steps assumeRole to get temporary credentials for a session client I get. But sp-api is not in the list of aws services to handle with boto3. Is there a reference for the sp-api to use with python or what would be the equivalent to s3 = boto3.client('s3') for the sp-api?
I had also issues and questions like you have, I managed to connect successfully, maybe this can help:
import boto3
# ======== GET AUTH ========
# Credentials for user created following the docs
amw_client = boto3.client(
'sts',
aws_access_key_id=self.access_key,
aws_secret_access_key=self.secret_key,
region_name=self.region
)
# ROLE created following the docs
# STS assume policy must be included in the role
res = amw_client.assume_role(
RoleArn='arn:aws:iam::xxxx:role/xxxx',
RoleSessionName='SellingPartnerAPI'
)
Credentials = res["Credentials"]
AccessKeyId = Credentials["AccessKeyId"]
SecretAccessKey = Credentials["SecretAccessKey"]
SessionToken = Credentials["SessionToken"]
from requests_auth_aws_sigv4 import AWSSigV4
aws_auth = AWSSigV4('execute-api',
aws_access_key_id=AccessKeyId,
aws_secret_access_key=SecretAccessKey,
aws_session_token=SessionToken,
region=self.region
)
import requests
# ======== GET ACCESS TOKEN ======
body = \
{
'grant_type': 'refresh_token',
'client_id': amazon_app_client_id,
'refresh_token': amazon_app_refresh_token,
'client_secret': amazon_app_client_secret
}
h = {'Content-Type': 'application/json'}
access_token_response = \
requests.post('https://api.amazon.com/auth/o2/token', json=body, headers=h)
access_token = self.access_token_response.json().get('access_token')
# ======== CONSUME API ========
resp = requests.get(
request_url, auth=aws_auth, headers={'x-amz-access-token': access_token})
Let me know if I can help further :)

when connecting to coinbase (and coinbase sandbox) getting {'message': 'Invalid API Key'} errors with Python code

I am using the following demo code (written in Python 3.x) to attempt to connect to the Coinbase Sandbox. Below is the code I have been following. I keep getting {'message': 'Invalid API Key'} errors. I have created API keys twice on the sandbox site: https://public.sandbox.pro.coinbase.com/ but nothing is working.
What am I doing wrong? Any help, hints or advice would be appreciated.
TIA
import json, hmac, hashlib, time, requests, base64
from requests.auth import AuthBase
# Before implementation, set environmental variables with the names API_KEY and API_SECRET
APIKEY = 'XXXXXXX-API'
API_PASS = 'rXXd0XX8XX'
API_SECRET = b'h2IUKbXXXXXXXXXXKL2d9XXXXXXXXXXWde5u+zcXXXXXXXXXXXXGJ8YqD8TXXXXXXXXXXXXXXX3dqM8pXXXXX8w=='
# Create custom authentication for Exchange
class CoinbaseExchangeAuth(AuthBase):
def __init__(self, api_key, secret_key, passphrase):
self.api_key = api_key
self.secret_key = secret_key
self.passphrase = passphrase
def __call__(self, request):
timestamp = str(time.time())
message = timestamp + request.method + request.path_url + (request.body or b'').decode()
hmac_key = base64.b64decode(self.secret_key)
signature = hmac.new(hmac_key, message.encode(), hashlib.sha256)
signature_b64 = base64.b64encode(signature.digest()).decode()
request.headers.update({
'CB-ACCESS-SIGN': signature_b64,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': self.api_key,
'CB-ACCESS-PASSPHRASE': self.passphrase,
'Content-Type': 'application/json'
})
return request
api_url = 'https://api-public.sandbox.pro.coinbase.com/'
auth = CoinbaseExchangeAuth(APIKEY, API_SECRET, API_PASS)
# Get accounts
r = requests.get(api_url + 'accounts', auth=auth)
print(r.json())
# [{"id": "a1b2c3d4", "balance":...
# Place an order
order = {
'size': 1.0,
'price': 1.0,
'side': 'buy',
'product_id': 'BTC-USD',
}
r = requests.post(api_url + 'orders', json=order, auth=auth)
print(r.json())
My mistake. Was not using the right number for the API Key
BTW, just for anyone else coming here: the Sandbox environment has DIFFERENT authorization keys, so you can't use the normal API Key, you have to create a new one. :)
I have had trouble finding a working solution in Python to get Coinbase Pro to authorize my API key I created. The original posters code worked with my API key values and changing out of the Sandbox environment (I am not using Sandbox, for this question it is appropriate value).
api_url = 'https://api.exchange.coinbase.com/'
Every other post I tried would give me stuff like:
key: expected bytes or bytearray, but got 'str'
I think this issue was resolved with the b in the line:
API_SECRET = b'h2IUKbXXXXXXXXXXKL2d9XXXXXXXXXXWde5u+zcXXXXXXXXXXXXGJ8YqD8TXXXXXXXXXXXXXXX3dqM8pXXXXX8w=='
making it the expected bytes instead of str. Thank you for this post.

boto3 unknownservice "mturk"

I am trying to use boto3 for Amazon Mechanical Turk. I was trying to get the client using the following code:
import boto3
endpoint_url = 'https://mturk-requester.us-east-1.amazonaws.com'
aws_access_key_id = <aws_access_key_id>
aws_secret_access_key = <aws_secret_access_key>
region_name = 'us-east-1'
client = boto3.client('mturk',
aws_access_key_id = aws_access_key_id,
aws_secret_access_key = aws_secret_access_key,
region_name=region_name,
endpoint_url = endpoint_url
)
But I am getting the following error about UnknownService name:
botocore.exceptions.UnknownServiceError: Unknown service: 'mturk'. Valid service
names are: acm,..., xray
Why is 'mturk' not in this list? The code I am using is taken from mturk developer website.
Any suggestion is welcome! Thanks in advance!

Boto3 ec2 describe_instances always returns empty

I have a very simple sandbox I'm trying to get to work so I can use it in a bigger application:
ec2_client = boto3.client(
'ec2',
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY)
response = ec2_client.describe_instances()
print(response)
and it results in...
{
'Reservations': [],
'ResponseMetadata': {
'RequestId': '2c28e8aa-da6d-4ca4-8ea7-f672518cac9f',
'HTTPStatusCode': 200,
'HTTPHeaders': {
'content-type': 'text/xml;charset=UTF-8',
'transfer-encoding': 'chunked',
'vary': 'Accept-Encoding',
'date': 'Thu, 07 Dec 2017 16:44:30 GMT',
'server': 'AmazonEC2'
},
'RetryAttempts': 0}
}
But the problem is no matter how many times I run this Reservations is ALWAYS empty :(.
In AWS consonle I can CLEARLLY see an instance is running...
I tried starting more instances, restarting the instances I had running. I put my initial script in a loop and ran it on repeat while I did this looking for any sign of the Reservations array actually having data.
I double checked that my aws ACCESS_KEY and SECRET_KEY are both correct and pointing to the correct account. They are.
I have no clue why this is. Its so simple and should be working. I'm new to AWS so any help is appreciated.
Seems that you forgot to add the region.
Set the region when creating your client
ec2_client = boto3.client(
'ec2',
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
region_name=REGION_NAME
)
response = ec2_client.describe_instances()
print(response)
If your EC2 instances are in Oregon, you can do region_name='us-west-2'
Hard coding credentials is not recommended. You can configure your profiles using the awscli and then reference it in your code.
session = boto3.Session(profile_name='dev')
# Any clients created from this session will use credentials
# from the [dev] section of ~/.aws/credentials.
ec2_client = session.client('ec2')
You can read more about Boto3 credentials Boto3 Credentials
The problem is that somehow boto3 was using a region that my instance was not running in. The solution was to specify the region when initializing the client:
ec2_client = boto3.client(
'ec2',
region_name='us-east-2',
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY)
All credit to #kichik for telling me where to look!

Generate S3 pre-signed URL with v4 signature using python boto3

The following does not work:
From the boto 3 docs:
http://boto3.readthedocs.io/en/latest/guide/s3.html#generating-presigned-urls
This is my script with placeholder bucket and key values:
import boto3
import requests
from botocore.client import Config
# Get the service client.
s3 = boto3.client('s3', config=Config(signature_version='s3v4'))
# Generate the URL to get 'key-name' from 'bucket-name'
url = s3.generate_presigned_url(
ClientMethod='get_object',
Params={
'Bucket': 'mybucketname',
'Key': 'myObject.txt'
}
)
print url
response = requests.get(url)
print response
S3 responds with a 403:
<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>B5681E888657E2A1</RequestId>
<HostId>
FMS7oPPOXt4I0KXPPQwdBx2fyxze+ussMmy/BOWLVFusWMoU2zAErE08ez34O6VhSYRvIYFm7Bs=
</HostId>
</Error>
You need to provide aws credentials with your boto3 client. Docs here
If you need help getting access to your credentials on aws you can look here.
import boto3
client = boto3.client(
's3',
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
aws_session_token=SESSION_TOKEN,
)
# Or via the Session
session = boto3.Session(
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
aws_session_token=SESSION_TOKEN,
)

Categories

Resources