【问题标题】:How do I get a refresh token for command line gsutil to work?如何获取命令行 gsutil 工作的刷新令牌?
【发布时间】:2014-06-23 20:18:28
【问题描述】:

我使用 gsutil 将文件从 Windows 机器传输到 Google Cloud Storage。

我已经超过 6 个月没有使用它了,现在当我尝试它时,我得到:

失败:invalid_grant

通过研究,我怀疑访问令牌不再有效,因为它已经 6 个月未使用,我需要刷新令牌?

我似乎找不到如何获取和使用它。

谢谢

运行 gsutil -DD config 会产生以下输出:

C:\Python27>python c:/gsutil/gsutil -DD config 

DEBUG:boto:path=/pub/gsutil.tar.gz
DEBUG:boto:auth_path=/pub/gsutil.tar.gz
DEBUG:boto:Method: HEAD
DEBUG:boto:Path: /pub/gsutil.tar.gz
DEBUG:boto:Data:
DEBUG:boto:Headers: {}
DEBUG:boto:Host: storage.googleapis.com
DEBUG:boto:Params: {}
DEBUG:boto:establishing HTTPS connection: host=storage.googleapis.com, kwargs={'timeout': 70}
DEBUG:boto:Token: None
DEBUG:oauth2_client:GetAccessToken: checking cache for key *******************************
DEBUG:oauth2_client:FileSystemTokenCache.GetToken: key=******************************* not present (cache_file= c:\users\admini~1\appdata\local\temp\2\oauth2_client-tokencache._.ea******************************)
DEBUG:oauth2_client:GetAccessToken: token from cache: None
DEBUG:oauth2_client:GetAccessToken: fetching fresh access token...
INFO:oauth2client.client:Refreshing access_token connect: (accounts.google.com, 443) 
send: 'POST /o/oauth2/token HTTP/1.1\r\nHost: accounts.google.com\r\nContent-Length: 177\r\ncontent-type: application/x- www-form-urlencoded\r\naccept-encoding: gzip, deflate\r\nuser-agent: Python-httplib2/0.7.7 (gzip)\r\n\r\nclient_secret=******************&grant_type=refresh_token&refresh_token=****************************************&client_ id=****************.apps.googleusercontent.com' reply: 'HTTP/1.1 400 Bad Request\r\n' 
header: Content-Type: application/json; charset=utf-8 header: Cache-Control: no-cache, no-store, max-age=0, must-revalidate header: Pragma: no-cache header: Expires: Fri, 01 Jan 1990 00:00:00 GMT header: Date: Thu, 08 May 2014 02:02:21 GMT header: Content-Disposition: attachment; filename="json.txt"; filename*=UTF-8''json.txt header: Content-Encoding: gzip header: X-Content-Type-Options: nosniff header: X-Frame-Options: SAMEORIGIN
header: X-XSS-Protection: 1; mode=block header: Server: GSE header: Alternate-Protocol: 443:quic header: Transfer-Encoding: chunked
INFO:oauth2client.client:Failed to retrieve access token: { "error" : "invalid_grant" } 
Traceback (most recent call last):
  File "c:/gsutil/gsutil", line 83, in <module> gslib.__main__.main() File "c:\gsutil\gslib_main_.py", line 151, in main command_runner.RunNamedCommand('ver', ['-l']) 
  File "c:\gsutil\gslib\command_runner.py", line 95, in RunNamedCommand self._MaybeCheckForAndOfferSoftwareUpdate(command_name, debug)):
  File "c:\gsutil\gslib\command_runner.py", line 181, in _MaybeCheckForAndOfferSoftwareUpdate cur_ver = LookUpGsutilVersion(suri_builder.StorageUri(GSUTIL_PUB_TARBALL)) 
  File "c:\gsutil\gslib\util.py", line 299, in LookUpGsutilVersion obj = uri.get_key(False) 
  File "c:\gsutil\third_party\boto\boto\storage_uri.py", line 342, in get_key generation=self.generation) 
  File "c:\gsutil\third_party\boto\boto\gs\bucket.py", line 102, in get_key query_args_l=query_args_l) 
  File "c:\gsutil\third_party\boto\boto\s3\bucket.py", line 176, in _get_key_internal query_args=query_args) 
  File "c:\gsutil\third_party\boto\boto\s3\connection.py", line 547, in make_request retry_handler=retry_handler 
  File "c:\gsutil\third_party\boto\boto\connection.py", line 947, in make_request retry_handler=retry_handler) 
  File "c:\gsutil\third_party\boto\boto\connection.py", line 838, in _mexe request.authorize(connection=self) 
  File "c:\gsutil\third_party\boto\boto\connection.py", line 377, in authorize connection._auth_handler.add_auth(self, *********) 
  File "c:\gsutil\gslib\third_party\oauth2_plugin\oauth2_plugin.py", line 22, in add_auth self.oauth2_client.GetAuthorizationHeader() 
  File "c:\gsutil\gslib\third_party\oauth2_plugin\oauth2_client.py", line 338, in GetAuthorizationHeader return 'Bearer %s' % self.GetAccessToken().token 
  File "c:\gsutil\gslib\third_party\oauth2_plugin\oauth2_client.py", line 309, in GetAccessToken access_token = self.FetchAccessToken() 
  File "c:\gsutil\gslib\third_party\oauth2_plugin\oauth2_client.py", line 435, in FetchAccessToken credentials.refresh(http) 
  File "c:\gsutil\third_party\google-api-python-client\oauth2client\client.py", line 516, in refresh self._refresh(http.request) 
  File "c:\gsutil\third_party\google-api-python-client\oauth2client\client.py", line 653, in _refresh self._do_refresh_request(http_request) 
  File "c:\gsutil\third_party\google-api-python-client\oauth2client\client.py", line 710, in _do_refresh_request raise AccessTokenRefreshError(error_msg) oauth2client.client.AccessTokenRefreshError: invalid_grant

【问题讨论】:

    标签: google-cloud-storage gsutil


    【解决方案1】:

    您可以要求 gsutil 自行配置。使用 gsutil 进入目录并运行:

    c:\gsutil> python gsutil config
    

    Gsutil 将引导您完成设置凭据的步骤。

    也就是说,访问令牌通常只持续大约半小时。以前配置的刷新令牌更有可能由于某种原因被撤销。或者,您只能以特定速率请求新令牌。您的帐户可能由于某种原因请求了很多很多刷新令牌,并且暂时受到访问服务的速率限制。

    【讨论】:

    • 感谢 Brandon - 我昨天尝试了配置并得到:C:\gsutil>python gsutil config 失败:invalid_grant。我刚刚又试了一次,奇怪的是我得到了:C:\Python27>python c:/gsutil/gsutil config Failure: Unable to find the server at accounts.google.com。
    • 这很奇怪。它还在发生吗?您是否可能位于阻止您访问 accounts.google.com 的防火墙后面?你能用那台机器上的网络浏览器查看这个 URL:accounts.google.com/ServiceLogin?hl=en
    • 嗨 Brandon - 是的,我可以使用我的登录名通过 Chrome 访问帐户网址。
    • 使用 C:\Python27>python c:/gsutil/gsutil config - 我现在又回到了 invalid_grant
    • 您能否再次尝试使用gsutil -DD config 并将结果发布到您的问题中?其中可能会有几行包括“授权:SomeLongStringOfStuff”。确保在发布之前删除这些行。
    【解决方案2】:

    现在是验证命令

    $ gcloud auth login
    

    这应该会刷新您的资助并让您重新开始。

    你可能还想跑

    $ gcloud components update
    

    更新您的安装。

    【讨论】:

      【解决方案3】:

      Brandon Yarbrough 给了我解决这个问题的建议。他怀疑 .boto 文件已损坏,并建议我删除它并再次运行 gsutil config。我这样做了,它解决了这个问题。

      【讨论】:

      • 仅供参考:当前版本的 gsutil 不易受到此问题的影响。
      猜你喜欢
      • 2017-01-08
      • 2021-08-25
      • 2019-05-02
      • 2020-02-26
      • 2019-09-09
      • 2014-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多