在Python中验证用户以检索Google联系人

2024-05-18 23:08:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我想获取谷歌联系人,为此我遵循这个文件。为了验证用户身份,我正在使用OAuth2.0。但我无法验证我的用户。你知道吗

    client_id = 'abcedf'
    client_secret= 'qwert'
    scope=(
            'https://www.googleapis.com/auth/contacts.readonly'
        ),


credentials = {
  'client_id':client_id,
  'client_secret':client_secret,
  'scope':scope
}

auth2token = gdata.gauth.OAuth2TokenFromCredentials(credentials)

但是上面的身份验证不起作用,所以我无法继续检索联系人。你知道吗

enter image description here


Tags: 文件用户httpscomclientidsecretwww
1条回答
网友
1楼 · 发布于 2024-05-18 23:08:24

由于您使用Python对用户进行身份验证,因此可以尝试使用oauth2client-library for Python说明如何在身份验证中使用流对象:

flow_from_clientsecrets()

oauth2client.client.flow来自clientsecrets()方法从客户端创建流对象_机密.json文件。这个JSON格式的文件存储您的客户机ID、客户机secret和其他oauth2.0参数。你知道吗

下面显示了如何使用flow_from_clientsecrets()创建流对象:

使用来自\u clientsecrets()的流

from oauth2client.client import flow_from_clientsecrets
...
flow = flow_from_clientsecrets('path_to_directory/client_secrets.json',
                               scope='https://www.googleapis.com/auth/calendar',
                               redirect_uri='http://example.com/auth_return')

使用OAuth2WebServerFlow

不管它的名字是什么,oauth2client.client.OAuth2WebServerFlow类同时用于已安装的应用程序和web应用程序。它是通过将客户机ID、客户机secret和作用域传递给它的构造函数来创建的:为构造函数提供一个redirect_uri参数。这必须是应用程序处理的URI。你知道吗

from oauth2client.client import OAuth2WebServerFlow
...
flow = OAuth2WebServerFlow(client_id='your_client_id',
                           client_secret='your_client_secret',
                           scope='https://www.googleapis.com/auth/calendar',
                           redirect_uri='http://example.com/auth_return')

相关问题 更多 >

    热门问题