Google 数据存储 API Python 中的身份验证

Google Datastore API Authentication in Python

验证请求,尤其是 Google 的 API 非常令人困惑!

我想通过 python 发出授权的 HTTP POST 请求,以便从数据存储中查询数据。我已经准备好一个服务帐户和 p12 文件。我看过示例,但似乎无论我尝试哪一个,我总是没有权限提出请求。

在浏览器中一切正常,所以我知道我的权限是正确的。所以我想我的问题是,如何通过 python?

从数据存储 API 安全地验证和请求数据

我好迷茫...

您可能不应该使用原始 POST 请求来使用 Datastore,而是使用 gcloud library 为您完成繁重的工作。

我也推荐 Python getting started page,因为它有一些很好的教程。

最后,我录制了一个播客,其中介绍了 using Datastore with Python 的基础知识,请查看!

Here is the code,这里是一个例子:

#Import libraries
from gcloud import datastore
import os

#The next few lines will set up your environment variables
#Replace "YOUR_RPOEJCT_ID_HERE" with the correct value in code.py
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "key.json"

projectID = "YOUR_RPOEJCT_ID_HERE"

os.environ["GCLOUD_TESTS_PROJECT_ID"] = projectID
os.environ["GCLOUD_TESTS_DATASET_ID"] = projectID
datastore.set_default_dataset_id(projectID)

#Let us build a message board / news website

#First, create a fake email for our fake user
email = "me@fake.com"

#Now, create a 'key' for that user using the email
user_key = datastore.Key('User', email)

#Now create a entity using that key
new_user = datastore.Entity( key=user_key )

#Add some fields to the entity

new_user["name"] = unicode("Iam Fake")
new_user["email"] = unicode(email)

#Push entity to the Cloud Datastore
datastore.put( new_user )

#Get the user from datastore and print
print( datastore.get(user_key) )

此代码已在 Apache v2 下获得许可