如何从谷歌云控制台项目中获取正确的JSON凭据文件?

huangapple go评论69阅读模式
英文:

How to get the right JSON credentials file from google cloud console project?

问题

I am trying to authenticate my flask web app to send password reset emails using the google Gmail API, I have already set up my project and acquired my credentials json file in the google console. When I run my code, the following method in the class handling the whole authentication process catches the DefaultCredentialsError

def get_credentials(self, authorization_code=None):
    if self.credentials is not None:
        return self.credentials

    # Load the saved credentials
    try:
        creds = google.auth.load_credentials_from_file(Config.Config.PATH)
    except DefaultCredentialsError:
        flash('SETUP INCOMPLETE', 'warning')
        return redirect(url_for('home'))

the problem is with the file having a type None instead of one of the expected file types,,, this is the error message from the browser

google.auth.exceptions.DefaultCredentialsError: The file C:\Users\MARK KARIUKI\OneDrive\Documents/googleDocs/creds.json does not have a valid type. Type is None, expected one of ('authorized_user', 'service_account', 'external_account', 'external_account_authorized_user', 'impersonated_service_account', 'gdch_service_account').

how can I get a json file that has the required type? ,,, for my application I need the 'authorised user' type.

I have tried recreating the credentials a couple of times but the same details are included in the json file and no type key is included in the dictionary contained in the json file.

英文:

I am trying to authenticate my flask web app to send password reset emails using the google Gmail API, I have already set up my project and acquired my credentials json file in the google console. When I run my code, the following method in the class handling the whole authentication process catches the DefaultCredentialsError


def get_credentials(self, authorization_code=None):
    if self.credentials is not None:
        return self.credentials

    # Load the saved credentials
    try:
        creds = google.auth.load_credentials_from_file(Config.Config.PATH)
    except DefaultCredentialsError:
        flash('SETUP INCOMPLETE', 'warning')
        return redirect(url_for('home'))

the problem is with the file having a type None instead of one of the expected file types,,, this is the error message from the browser

google.auth.exceptions.DefaultCredentialsError: The file C:\Users\MARK KARIUKI\OneDrive\Documents/googleDocs/creds.json does not have a valid type. Type is None, expected one of ('authorized_user', 'service_account', 'external_account', 'external_account_authorized_user', 'impersonated_service_account', 'gdch_service_account').

how can I get a json file that has the required type? ,,, for my application I need the 'authorised user' type.

I have tried recreating the credentials a couple of time but the same details are included in the json file and no type key is included in the dictionary contained in the json file.

答案1

得分: 0

如果您正在创建一个Flask Web应用程序,那么您可能想要创建Web应用程序凭据。这将使用Oauth2来请求用户同意访问其Gmail帐户并代表他们发送电子邮件。

您似乎可能已经创建了服务帐户凭据。服务帐户就像一个虚拟用户,如果您在Google Workspace帐户上配置了域广泛的委派,那么您可以冒充域内的任何用户并代表他们发送电子邮件。但是,授权服务帐户的代码与用于Oauth2授权的代码不同。

credentials = ServiceAccountCredentials.from_json_keyfile_name(
    SERVICE_ACCOUNT_FILE_PATH,
    scopes=SCOPES)

credentials = credentials.create_delegated(user_email)

请记住,只有在与Google Workspace域帐户一起使用时,服务帐户才能与Gmail API一起使用。

英文:

If you are creating a flask web app then you would probably want to create web application credentials. This will use Oauth2 to request consent of the user to access their gmail account and send emails on their behalf.

You appear to have maybe created service account credentials. A service account is like a dummy user if you configure domain wide delegation on your google workspace account you can then impersonate any user on the domain and send emails on their behalf. however the code to authorize a service account is different then the code used for Oauth2 authorization

credentials = ServiceAccountCredentials.from_json_keyfile_name(
    SERVICE_ACCOUNT_FILE_PATH,
    scopes=SCOPES)

credentials = credentials.create_delegated(user_email)

Remember service accounts only work with the gmail api if you use them with a google workspace domain account.

huangapple
  • 本文由 发表于 2023年5月20日 22:10:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定