【发布时间】:2020-09-09 05:07:05
【问题描述】:
我尝试制作微服务器来连接 google people API。 我在我的本地主机'MACOS'中做,一切正常。 但是在我部署到服务器“CENTOS”之后,服务器无法呈现身份验证 URL: 构建谷歌服务的代码
def contact_service_build(pickle_file_name):
"""Shows basic usage of the People API.
Prints the name of the first 10 connections.
"""
path = str(dir.dir)
token_file = path + '/project/google_auth/'+pickle_file_name+ '.pickle'
secrets_file = path + '/project/credentials/credentials.json'
SCOPES = ['https://www.googleapis.com/auth/contacts']
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists(token_file):
with open(token_file, 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
print('not creds or not creds.valid')
if creds and creds.expired and creds.refresh_token:
print('creds and creds.expired and creds.refresh_token')
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(secrets_file, SCOPES)
creds = flow.run_local_server(port=0)
print('creds = flow.run_local_server(port=0)')
# Save the credentials for the next run
with open(token_file, 'wb') as token:
pickle.dump(creds, token)
service = build('people', 'v1', credentials=creds)
return service
与谷歌联系的代码
def creat_google_contact(service, first_name, last_name, phone, email, streetAddress, extendedAddress, city, zip,
province,
country, country_code, biographies, company):
# POST / v1 / people: createContact
# HTTP / 1.1
# Body: {"names": [{"givenName": "John", "familyName": "Doe"}]}
# Host: people.googleapis.comcompany
# service = discovery.build('people', 'v1', http=http, discoveryServiceUrl='https://people.googleapis.com/$discovery/rest')
try:
service.people().createContact(body={
"organizations": [
{
"name": str(company)
}
],
"biographies": [
{
"value": str(biographies),
"contentType": 'TEXT_HTML'
}
],
"names": [
{
"givenName": str(last_name),
"familyName": str(first_name)
}
],
"phoneNumbers": [
{
'value': str(phone)
}
],
"emailAddresses": [
{
'value': str(email)
}
],
"addresses": [
{
"streetAddress": str(streetAddress),
"extendedAddress": str(extendedAddress),
"city": str(city),
"region": str(province),
"postalCode": str(zip),
"country": str(country),
"countryCode": str(country_code)
}
]
}).execute()
print("da ghi lenh danh ba gooogle", first_name, last_name)
except Exception as e:
print('khong ghi duoc danh ba',first_name,last_name)
pass
在 SSH 终端中我可以看到 python 打印出来
127.0.0.1 - - [09/Sep/2020 12:04:30] "GET /sync_contact HTTP/1.0" 200 -
not creds or not creds.valid
Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=44359f0pm.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A36883%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcontacts&state=7mjA48LHGRZnRT&access_type=offline
但我不知道如何在网络浏览器中打开此链接????
【问题讨论】:
-
嗨,您是否尝试过在本地机器上进行 OAuth 并通过 ssh 发送令牌?
-
谢谢你,我找到了办法:)我已经更新了解决方案
标签: python-3.x flask service flow google-people-api