HttpError 401尝试通过CloudStorage管道访问BigQuery URL

时间:2012-12-21 03:56:55

标签: python google-app-engine google-bigquery google-cloud-storage google-api-python-client

这个问题基本上是我之前提出的here问题的后续问题。在处理了我遇到的编码错误之后,我遇到了一个新的HttpError 401问题。我的开发者应用服务器日志中的跟踪如下:

ERROR    2012-12-20 03:10:46,312 pipeline.py:2237] Generator main.CloudStorageToBigQuery(*([u'/gs/jibdancsvtest/Datastore Mapper main.streamdata-1582389642294FE5DBBCE-output'],), **{})#2d3dba4f4a5211e2bf0ac3cee1488e47 raised exception. HttpError: <HttpError 401 when requesting https://www.googleapis.com/bigquery/v2/projects/1093XXXXXXXXX/jobs?alt=json returned "Invalid Credentials">
Traceback (most recent call last):
  File "C:\Users\Tank\Documents\Aptana Studio 3 Workspace\jibdantest-bq\mapreduce\lib\pipeline\pipeline.py", line 2030, in evaluate
    self, pipeline_key, root_pipeline_key, caller_output)
  File "C:\Users\Tank\Documents\Aptana Studio 3 Workspace\jibdantest-bq\mapreduce\lib\pipeline\pipeline.py", line 1064, in _run_internal
    return self.run(*self.args, **self.kwargs)
  File "C:\Users\Tank\Documents\Aptana Studio 3 Workspace\jibdantest-bq\main.py", line 77, in run
    result.execute()
  File "C:\Users\Tank\Documents\Aptana Studio 3 Workspace\jibdantest-bq\oauth2client\util.py", line 120, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "C:\Users\Tank\Documents\Aptana Studio 3 Workspace\jibdantest-bq\apiclient\http.py", line 678, in execute
    raise HttpError(resp, content, uri=self.uri)
HttpError: <HttpError 401 when requesting https://www.googleapis.com/bigquery/v2/projects/1093XXXXXXXXX/jobs?alt=json returned "Invalid Credentials">

我还以为我会从网址发布json响应。这是json:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
 }
}

如果您在stackoverflow上点击我之前的问题链接,您将在我的main.py文件中找到运行所有内容的代码。但如果我需要在这里重新发布,我可以这样做。

所以,我认为这是一个OAuth问题,但我相信我已经正确设置了所有这些问题。对我可以做/调查的其他事情的任何建议都会很棒。

非常感谢提前。

2 个答案:

答案 0 :(得分:2)

在本地开发服务器上运行时,需要以不同方式设置凭据:

首先,您需要设置本地凭据存储。从这里使用get_refresh_token.py:https://codereview.appspot.com/5362041/diff/1/samples/oauth2cmdline/get_refresh_token.py

在Google API控制台上为网络应用程序设置客户端ID,并将参数传递给上面的脚本(将启动到浏览器进行OAuth2身份验证)。这将创建一个包含凭据的本地文件,然后您可以将其传递给API客户端以获取经过身份验证的请求:

from oauth2client.client import Storage, Credentials
from oauth2client.appengine import AppAssertionCredentials

class FileStorage(Storage):
    def __init__(self, filepath):
        self._filepath = filepath

    def locked_get(self):
        with open(self._filepath, 'r') as f:
            json = f.read()
        credentials = Credentials.new_from_json(json)
        return credentials

if not os.environ.get('SERVER_SOFTWARE', '').startswith('Development'):
    credentials = AppAssertionCredentials(scope='https://www.googleapis.com/auth/bigquery')
else:
    filepath = #Path to credentials file here
    storage = FileStorage(filepath)
    credentials = storage.get()

希望这有帮助!

答案 1 :(得分:1)

您可能想要检查您是否拥有运行作业的正确权限。我们遇到了类似的问题,发现我们需要所有者许可来运行这些工作。

希望它有所帮助!