Python Google功能无法使用oauth2client中的服务帐户

Python Google功能无法使用oauth2client中的服务帐户,python,google-cloud-platform,google-cloud-functions,google-sheets-api,Python,Google Cloud Platform,Google Cloud Functions,Google Sheets Api,我在我的windows中使用了以下代码,但一旦我将其作为云函数部署到GCP上,就会出现此错误?我怎样才能避开它?我想做的是阅读一个谷歌表单,并在我的函数中使用它 from google.oauth2 import service_account credentials = service_account.Credentials.from_service_account_file( SERVICE_ACCOUNT_FILE, scopes=SCOPES) delega

我在我的windows中使用了以下代码,但一旦我将其作为云函数部署到GCP上,就会出现此错误?我怎样才能避开它?我想做的是阅读一个谷歌表单,并在我的函数中使用它

from google.oauth2 import service_account
  credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)

    delegated_credentials = credentials.with_subject(EMAIL_FROM)
google函数日志给出了以下错误

textPayload:ModuleNotFoundError:没有名为“oauth2client”的模块 ImportError:使用oauth2client>=4.0.0或google auth时,文件\u缓存不可用


如何解决它?

您必须更改依赖关系oauth2client,正如前面提到的,它已被弃用

您可以选择GoogleAuth==1.13.1,它应该可以工作


所以,你的下一个问题是:为什么它能在我的本地环境下工作?我认为您已经在全球范围内安装了GoogleAuth,并且您的代码采用这种依赖关系,即使它不在requirements.txt中。我建议您使用虚拟环境,并仅在此venv中安装依赖项。

希望这有帮助,顺便说一句,目标受众是您想要访问的url

import argparse
import google.auth
import google.auth.app_engine
import google.auth.compute_engine.credentials
import google.auth.iam
from google.auth.transport.requests import Request
import google.oauth2.credentials
from google.oauth2 import service_account

class AuthenticationConstants:
    AUTHENTICATION_SCOPES_URL = 'https://www.googleapis.com/auth/cloud-platform'
    OAUTH_TOKEN_URI = 'https://www.googleapis.com/oauth2/v4/token'

class JWT(object):
    def __init__(self, service_account_key_path):
        self.service_account_key_path = service_account_key_path
        self.credentials = service_account.Credentials.from_service_account_file(
            self.service_account_key_path)
        self.scoped_credentials = self.credentials.with_scopes(
            [AuthenticationConstants.OAUTH_TOKEN_URI])

    def get_google_open_id_connect_token(self, target_audience):
        signer_email = self.scoped_credentials.service_account_email
        signer = self.scoped_credentials.signer

        service_account_credentials = google.oauth2.service_account.Credentials(
            signer,
            signer_email,
            token_uri=AuthenticationConstants.OAUTH_TOKEN_URI,
            additional_claims={
                'target_audience': target_audience
            }
        )

        service_account_jwt = service_account_credentials._make_authorization_grant_assertion()
        request = google.auth.transport.requests.Request()
        body = {
            'assertion': service_account_jwt,
            'grant_type': google.oauth2._client._JWT_GRANT_TYPE,
        }
        token_response = google.oauth2._client._token_endpoint_request(
            request, AuthenticationConstants.OAUTH_TOKEN_URI, body)
        return token_response['id_token']

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--service-account-path")
    parser.add_argument("--target-audience")
    result = parser.parse_args()
    jwt = JWT(result.service_account_path)
    print(jwt.get_google_open_id_connect_token(result.target_audience))
以下是我使用的requirements.txt:

google-api-python-client==1.7.11

你的requirements.txt文件是什么?您是否在本地虚拟环境中运行代码?这是我在需求中所做的。txthttplib2==0.15.0 firebase_admin==3.2.1 oauth2client==4.1.3 google_api_python_client==1.8.0 protobuf==3.11.3我是从google云函数运行的