Python中的googledatastoreapi认证

2024-04-25 19:43:32 发布

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

对请求进行身份验证,尤其是使用Google的API进行身份验证是如此令人费解!在

我想通过python发出授权httppost请求,以便从数据存储中查询数据。我有一个服务帐户和p12文件都准备好了。我看过这些例子,但似乎无论我尝试哪一个,我总是无权提出请求。在

在浏览器中一切正常,所以我知道我的权限都是井然有序的。所以我想我的问题是,如何通过python从数据存储API安全地进行身份验证和请求数据?在

我太迷路了。。。在


Tags: 文件数据身份验证api权限google浏览器帐户
1条回答
网友
1楼 · 发布于 2024-04-25 19:43:32

您可能不应该使用原始POST请求来使用数据存储,而是使用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) )

此代码是在apachev2下授权的

相关问题 更多 >