G-Cloud SDK应用:如何授权sdk app访问google张

G-Cloud SDK Application: How to authorize sdk app to access google sheets

将应用程序部署到 Google 云时遇到问题。它在本地运行良好,但有一个 Google 云的身份验证问题。如何授权部署到 G-Cloud SDK 的应用程序使用 google 张?我尝试了多种方法,但都失败了。我认为我的痛点是我正在尝试进行身份验证,就好像这是一个客户端,但事实并非如此。我也尝试过服务帐户方式,但我可能搞砸了。下面是在本地工作但无法适应 Google SDK 的代码。

def get_entry():
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.pickle'):
    with open('token.pickle', '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:
    if creds and creds.expired and creds.refresh_token:
        creds.refresh(Request())
    else:
        flow = InstalledAppFlow.from_client_secrets_file(
            'client_secret.json', SCOPES)
        creds = flow.run_local_server(port=0)
    # Save the credentials for the next run
    with open('token.pickle', 'wb') as token:
        pickle.dump(creds, token)
service = build('sheets', 'v4', credentials=creds)

# Call the Sheets API
sheet = service.spreadsheets()

这是因为 运行 由于限制,您不能在 Google App Engine 上使用本地服务器。

我推荐你使用下面的代码:

if os.environ.get('GAE_ENV') == 'standard':
    creds = flow.run_console()
else:
    creds = flow.run_local_server()