Jos*_*osh 4 python google-app-engine
我正在尝试使用该remote_api_stub
方法运行具有远程api访问远程数据存储的localhost Web服务器ConfigureRemoteApiForOAuth
.
我一直在使用以下Google文档作为参考,但发现它相当稀疏:
https://cloud.google.com/appengine/docs/python/tools/remoteapi
我相信我错过了认证位,但找不到具体的资源来指导我.考虑到以下代码示例,在运行时访问远程数据存储区的最简单方法是什么dev_appserver.py
?
import webapp2
from google.appengine.ext import ndb
from google.appengine.ext.remote_api import remote_api_stub
class Topic(ndb.Model):
created_by = ndb.StringProperty()
subject = ndb.StringProperty()
@classmethod
def query_by_creator(cls, creator):
return cls.query(Topic.created_by == creator)
class MainPage(webapp2.RequestHandler):
def get(self):
remote_api_stub.ConfigureRemoteApiForOAuth(
'#####.appspot.com',
'/_ah/remote_api'
)
topics = Topic.query_by_creator('bill')
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('<html><body>')
self.response.out.write('<h1>TOPIC SUBJECTS:<h1>')
for topic in topics.fetch(10):
self.response.out.write('<h3>' + topic.subject + '<h3>')
self.response.out.write('</body></html>')
app = webapp2.WSGIApplication([
('/', MainPage)
], debug=True)
Run Code Online (Sandbox Code Playgroud)
这个问题很多,只是因为你不能在SDK之外使用app引擎库.但是,也可以从App Engine SDK中更轻松地完成此操作.
我会用gcloud
它.以下是如何设置它:
如果您想与App Engine环境内部或外部的Google云存储服务进行交互,您可以使用Gcloud(https://googlecloudplatform.github.io/gcloud-python/stable/)来执行此操作.
您需要在应用程序上使用服务帐户以及下载JSON凭证文件.您可以在authentication
选项卡下的应用引擎控制台上执行此操作.创建它,然后下载它.叫它client_secret.json
什么的.
有了这些,一旦你用pip为gcloud安装了正确的软件包,你就可以进行查询以及写入数据.
以下是验证自己使用库的示例:
from gcloud import datastore
# the location of the JSON file on your local machine
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/location/client_secret.json"
# project ID from the Developers Console
projectID = "THE_ID_OF_YOUR_PROJECT"
os.environ["GCLOUD_TESTS_PROJECT_ID"] = projectID
os.environ["GCLOUD_TESTS_DATASET_ID"] = projectID
client = datastore.Client(dataset_id=projectID)
Run Code Online (Sandbox Code Playgroud)
完成后,您可以进行以下查询:
query = client.query(kind='Model').fetch()
Run Code Online (Sandbox Code Playgroud)
这实际上非常简单.任何人,我就是这样做的!干杯.
归档时间: |
|
查看次数: |
795 次 |
最近记录: |