Python Google应用程序引擎上的Google Drive服务帐户访问以模拟用户
我正试图获得服务帐户访问谷歌驱动器API。在构建应用程序时,我遵循了。我的代码与示例几乎完全相似:Python Google应用程序引擎上的Google Drive服务帐户访问以模拟用户,python,google-app-engine,google-drive-api,google-api-python-client,Python,Google App Engine,Google Drive Api,Google Api Python Client,我正试图获得服务帐户访问谷歌驱动器API。在构建应用程序时,我遵循了。我的代码与示例几乎完全相似: class MainPage(webapp2.RequestHandler): def get(self): build = createDriveService(user) searchFile = build.files().get(fileId='FILEID').execute() self.response.write(searchFile)
class MainPage(webapp2.RequestHandler):
def get(self):
build = createDriveService(user)
searchFile = build.files().get(fileId='FILEID').execute()
self.response.write(searchFile)
def createDriveService(userEmail):
API_KEY = 'APIKEY'
credentials = AppAssertionCredentials(
scope='https://www.googleapis.com/auth/drive',
sub=userEmail)
http = httplib2.Http()
http = credentials.authorize(http)
return build('drive', 'v2', http=http, developerKey=API_KEY)
当我调用访问我的GAE页面时,我得到的日志中的错误是:
<"File not found: FILEID">
错误。从那以后,我确保在我的app.yaml
中包含pycrypto库。有什么想法吗
EDIT2
我已成功在app engine上模拟用户。我按照前面回答的问题进行了操作。我不理解您代码的用户部分,因为您使用的是Google App Engine项目用户帐户。 有关如何使用和查找此帐户的信息,请参阅。您还可以使用以下方式注销此帐户:
from google.appengine.api import app_identity
logging.info('service account : ' + app_identity.get_service_account_name())
确保您已授予此项目用户帐户访问驱动器文件或文件夹的权限
我的代码如下所示:
def createDriveService():
SCOPE = 'https://www.googleapis.com/auth/drive'
API_KEY = 'AIzaSyB9UkK4OH5Z_E4v3Qp6bay6QEgGpzou3bc' # GAE
credentials = AppAssertionCredentials(scope=SCOPE)
logging.info('using service account : ' + app_identity.get_service_account_name())
http = credentials.authorize(httplib2.Http())
return build('drive', 'v2', http=http, developerKey=API_KEY)
用户在我的代码中是一个变量的原因是,我正在镜像如何使用域范围的委派和模拟其他用户。确定。我不熟悉此选项。另外,当我使用代码时,我收到了相同的错误(在更改API密钥后),当您使用我的代码时,您是否将服务帐户添加到您的文件以允许其访问?是的,但这不适合我的使用情况。非常感谢。对用户模拟有什么见解吗?目前,我可以从终端使用SignedJwtAssertionCredentials运行我的程序,但是因为我希望应用程序在GAE上运行,所以我不能下载密钥。有什么想法吗?对我来说,关键信息是“sub”参数。谢谢
def createDriveService():
SCOPE = 'https://www.googleapis.com/auth/drive'
API_KEY = 'AIzaSyB9UkK4OH5Z_E4v3Qp6bay6QEgGpzou3bc' # GAE
credentials = AppAssertionCredentials(scope=SCOPE)
logging.info('using service account : ' + app_identity.get_service_account_name())
http = credentials.authorize(httplib2.Http())
return build('drive', 'v2', http=http, developerKey=API_KEY)