python – Google Contact API – Auth2.0
作者:互联网
我正在寻找一种从谷歌帐户中检索我的联系人的每个电子邮件地址的好方法,用于Python中的“桌面”应用程序.
我第一次通过Google Code创建了一个应用.我切换了Google Plus API,检索了我的大部分用户数据,但没有检索任何联系人.
我开始调查了,我发现了很多东西,但大多数都已经过时了.
我找到了一个很好的方法来检索我的联系人,使用gdata库,但通过https://www.google.com/m8/feeds给我一个完整的读/写访问权限,没有任何反馈.
self.gd_client = gdata.contacts.client.ContactsClient(source='MyAppliName')
self.gd_client.ClientLogin(email, password, self.gd_client.source)
据官方“谷歌联系api”谷歌组迁移到stackoverflow,只读访问被破坏.
顺便说一句,我不是’相信我的应用程序,我使用只读访问权限的巨大粉丝,我发誓.“
我在https://developers.google.com/oauthplayground找到了谷歌api游乐场,他们使用OAuth2.0令牌与大多数api,包括联系,切换网页:
Google OAuth 2.0 Playground is requesting permission to:
- Manage your contacts
根据这个游乐场,可以将OAuth2.0与google contact api一起使用,但我不知道如何将https:// www.google.com/m8/feeds添加到我的范围,该范围未显示在列表中.
还有其他办法吗?
解决方法:
如果这个问题仍然适合您,这里有一些示例代码如何使用oauth2和Google Contact API v3:
import gdata.contacts.client
from gdata.gauth import AuthSubToken
from oauth2client import tools
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
def oauth2_authorize_application(client_secret_file, scope, credential_cache_file='credentials_cache.json'):
"""
authorize an application to the requested scope by asking the user in a browser.
:param client_secret_file: json file containing the client secret for an offline application
:param scope: scope(s) to authorize the application for
:param credential_cache_file: if provided or not None, the credenials will be cached in a file.
The user does not need to be reauthenticated
:return OAuth2Credentials object
"""
FLOW = flow_from_clientsecrets(client_secret_file,
scope=scope)
storage = Storage(credential_cache_file)
credentials = storage.get()
if credentials is None or credentials.invalid:
# Run oauth2 flow with default arguments.
credentials = tools.run_flow(FLOW, storage, tools.argparser.parse_args([]))
return credentials
SCOPES = ['https://www.google.com/m8/feeds/', 'https://www.googleapis.com/auth/userinfo.email']
credentials = oauth2_authorize_application('client-secret.json', scope=SCOPES)
token_string = credentials.get_access_token().access_token
# deprecated!
# auth_token = AuthSubToken(token_string, SCOPES)
with open('client-secret.json') as f:
oauth2_client_secret = json.load(f)
auth_token = gdata.gauth.OAuth2Token(
client_id=oauth2_client_secret['web']['client_id'],
client_secret=oauth2_client_secret['web']['client_secret'],
scope=SCOPES,
user_agent='MyUserAgent/1.0',
access_token=credentials.get_access_token().access_token,
refresh_token=credentials.refresh_token)
client = gdata.contacts.client.ContactsClient(auth_token=auth_token)
query = gdata.contacts.client.ContactsQuery()
标签:python,google-api,oauth-2-0,google-contacts 来源: https://codeday.me/bug/20190620/1246559.html