尝试使用 Boto 在 MTurk 中获得平衡但得到 "The identity contained in the request is not authorized to use this AWSAccessKeyId"

Trying to Get Balance in MTurk using Boto but get "The identity contained in the request is not authorized to use this AWSAccessKeyId"

我正在尝试使用引用我的共享凭据文件的 Boto 在我的 Mechanical Turk 帐户上获取 GetAccountBalance。我通过直接输入我的凭据成功地做到了这一点,但在使用配置文件名称时却失败了。

以下代码有效

import import boto.mturk.connection
sandbox_host = 'mechanicalturk.sandbox.amazonaws.com'

mturk = boto.mturk.connection.MTurkConnection(
   aws_access_key_id = 'XXX',
   aws_secret_access_key = 'XXX',
   host = sandbox_host,
)

print mturk.get_account_balance() # [,000.00]

但是我想使用我在 ~/.aws/credentials 中创建的配置文件:

[default]
aws_access_key_id = 'XXX'
aws_secret_access_key = 'XXX'

[iamuser]
aws_access_key_id = 'XXX'
aws_secret_access_key = 'XXX'

默认配置文件是 AWS 主账户,而 iamuser 是具有 Full Mechanical Turk 权限的 IAM 用户。我想使用 iamuser 检查我的 MTurk 余额。正如我之前所说,前面带有显式键的代码工作正常。但是我希望它以下列方式工作:

import import boto.mturk.connection
sandbox_host = 'mechanicalturk.sandbox.amazonaws.com'

mturk = boto.mturk.connection.MTurkConnection(
   profile_name = 'iamuser',
   host = sandbox_host,
)

print mturk.get_account_balance() # [,000.00]

当我尝试此操作时,出现以下错误:

Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python2.7/site-packages/boto/mturk/connection.py", line 74, in get_account_balance ('OnHoldBalance', Price)]) File "/usr/local/lib/python2.7/site-packages/boto/mturk/connection.py", line 838, in _process_request return self._process_response(response, marker_elems) File "/usr/local/lib/python2.7/site-packages/boto/mturk/connection.py", line 853, in _process_response raise MTurkRequestError(response.status, response.reason, body) boto.mturk.connection.MTurkRequestError: MTurkRequestError: 200 OK b6bdb875-b937-471c-bc00-86225e198ee2AWS.NotAuthorizedThe identity contained in the request is not authorized to use this AWSAccessKeyId (1482788645643 s)

如果能就此问题提供帮助,我将不胜感激。 谢谢

找到我的错误所在。显然,读取凭据有优先级。环境变量比共享凭据具有更高的优先级。我在 .bash_profile 上声明了一些环境变量,这与我的脚本中的变量冲突。所以解决方案是从 .bash_profile 文件

中删除它们

对于发现这个错误非常有用的是 运行 python 上的以下命令:

boto.set_stream_logger('boto')

这使得调试 boto 变得更加容易。