google-api-python-client因OAuth2而被破坏?

时间:2016-02-19 06:03:38

标签: python oauth-2.0 google-bigquery google-cloud-storage google-api-client

我正在尝试使用Python中的Google Api Client检查bigquery中是否存在某个数据集。它总是工作,直到我得到这个奇怪的错误,我不知道如何修复的最后一次更新:

Traceback (most recent call last):
  File "/root/miniconda/lib/python2.7/site-packages/dsUtils/bq_utils.py", line 106, in _get
    resp = bq_service.datasets().get(projectId=self.project_id, datasetId=self.id).execute(num_retries=2)
  File "/root/miniconda/lib/python2.7/site-packages/oauth2client/util.py", line 140, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/root/miniconda/lib/python2.7/site-packages/googleapiclient/http.py", line 755, in execute
    method=str(self.method), body=self.body, headers=self.headers)
  File "/root/miniconda/lib/python2.7/site-packages/googleapiclient/http.py", line 93, in _retry_request
    resp, content = http.request(uri, method, *args, **kwargs)
  File "/root/miniconda/lib/python2.7/site-packages/oauth2client/client.py", line 598, in new_request
    self._refresh(request_orig)
  File "/root/miniconda/lib/python2.7/site-packages/oauth2client/client.py", line 864, in _refresh
    self._do_refresh_request(http_request)
  File "/root/miniconda/lib/python2.7/site-packages/oauth2client/client.py", line 891, in _do_refresh_request
    body = self._generate_refresh_request_body()
  File "/root/miniconda/lib/python2.7/site-packages/oauth2client/client.py", line 1597, in _generate_refresh_req
uest_body
    assertion = self._generate_assertion()
  File "/root/miniconda/lib/python2.7/site-packages/oauth2client/service_account.py", line 263, in _generate_ass
ertion
    key_id=self._private_key_id)
  File "/root/miniconda/lib/python2.7/site-packages/oauth2client/crypt.py", line 97, in make_signed_jwt
    signature = signer.sign(signing_input)
  File "/root/miniconda/lib/python2.7/site-packages/oauth2client/_pycrypto_crypt.py", line 101, in sign
    return PKCS1_v1_5.new(self._key).sign(SHA256.new(message))
  File "/root/miniconda/lib/python2.7/site-packages/Crypto/Signature/PKCS1_v1_5.py", line 112, in sign
    m = self._key.decrypt(em)
  File "/root/miniconda/lib/python2.7/site-packages/Crypto/PublicKey/RSA.py", line 174, in decrypt
    return pubkey.pubkey.decrypt(self, ciphertext)
  File "/root/miniconda/lib/python2.7/site-packages/Crypto/PublicKey/pubkey.py", line 93, in decrypt
    plaintext=self._decrypt(ciphertext)
  File "/root/miniconda/lib/python2.7/site-packages/Crypto/PublicKey/RSA.py", line 235, in _decrypt
    r = getRandomRange(1, self.key.n-1, randfunc=self._randfunc)
  File "/root/miniconda/lib/python2.7/site-packages/Crypto/Util/number.py", line 123, in getRandomRange
    value = getRandomInteger(bits, randfunc)
  File "/root/miniconda/lib/python2.7/site-packages/Crypto/Util/number.py", line 104, in getRandomInteger
    S = randfunc(N>>3)
  File "/root/miniconda/lib/python2.7/site-packages/Crypto/Random/_UserFriendlyRNG.py", line 202, in read
    return self._singleton.read(bytes)
  File "/root/miniconda/lib/python2.7/site-packages/Crypto/Random/_UserFriendlyRNG.py", line 178, in read
    return _UserFriendlyRNG.read(self, bytes)
  File "/root/miniconda/lib/python2.7/site-packages/Crypto/Random/_UserFriendlyRNG.py", line 137, in read
    self._check_pid()
  File "/root/miniconda/lib/python2.7/site-packages/Crypto/Random/_UserFriendlyRNG.py", line 153, in _check_pid
    raise AssertionError("PID check failed. RNG must be re-initialized after fork(). Hint: Try Random.atfork()")
AssertionError: PID check failed. RNG must be re-initialized after fork(). Hint: Try Random.atfork()

是否有人了解什么是hapening?

请注意,我也会像GCStorage这样的其他砖块出现此错误。

另请注意,我使用以下命令加载我的Google凭据:

from oauth2client.client import GoogleCredentials

def get_credentials(credentials_path):  #my json credentials path
    logger.info('Getting credentials...')
    try:
        os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credentials_path
        credentials = GoogleCredentials.get_application_default()
        return credentials
    except Exception as e:
        raise e

因此,如果有人知道使用我的json服务帐户文件加载我的Google凭据的更好方法,并且可以避免错误,请告诉我。

1 个答案:

答案 0 :(得分:1)

看起来错误出现在PyCrypto模块中,这似乎是由Google的OAuth2实现引起的。如果您的代码在某个时刻调用os.fork(),您可能需要在父进程和子进程中调用Crypto.Random.atfork()以更新模块的内部状态。

请参阅此处了解PyCrypto文档;搜索" atfork"了解更多信息: ACEAutocompleteBar

这个问题和答案也可能是相关的: https://github.com/dlitz/pycrypto