0

我有一个谷歌应用引擎网站,我想做的是访问我驱动器上的文件并发布它们。请注意,我的帐户同时拥有驱动器和应用程序引擎页面。

我曾尝试查看 google drive api,但问题是我不知道从他们文档中的以下样板代码开始。

如果你看一下这个函数:

def get_credentials(authorization_code, state):
    """Retrieve credentials using the provided authorization code.

    This function exchanges the authorization code for an access token and queries
    the UserInfo API to retrieve the user's e-mail address.
    If a refresh token has been retrieved along with an access token, it is stored
    in the application database using the user's e-mail address as key.
    If no refresh token has been retrieved, the function checks in the application
    database for one and returns it if found or raises a NoRefreshTokenException
    with the authorization URL to redirect the user to.

    Args:
      authorization_code: Authorization code to use to retrieve an access token.
      state: State to set to the authorization URL in case of error.
    Returns:
      oauth2client.client.OAuth2Credentials instance containing an access and
      refresh token.
    Raises:
      CodeExchangeError: Could not exchange the authorization code.
      NoRefreshTokenException: No refresh token could be retrieved from the
                               available sources.
    """
    email_address = ''
    try:
        credentials = exchange_code(authorization_code)
        user_info = get_user_info(credentials)
        email_address = user_info.get('email')
        user_id = user_info.get('id')
        if credentials.refresh_token is not None:
            store_credentials(user_id, credentials)
            return credentials
        else:
            credentials = get_stored_credentials(user_id)
            if credentials and credentials.refresh_token is not None:
                return credentials
    except CodeExchangeException, error:
        logging.error('An error occurred during code exchange.')
        # Drive apps should try to retrieve the user and credentials for the current
        # session.
        # If none is available, redirect the user to the authorization URL.
        error.authorization_url = get_authorization_url(email_address, state)
        raise error
    except NoUserIdException:
        logging.error('No user ID could be retrieved.')
        # No refresh token has been retrieved.
    authorization_url = get_authorization_url(email_address, state)
    raise NoRefreshTokenException(authorization_url)

这是样板代码的一部分。但是,我应该从哪里得到authorisation_code

4

1 回答 1

1

我最近不得不实现类似的东西,找到相关的文档非常棘手

这对我有用。

一次性设置为您的 Google App Engine 项目启用 Google Drive

  1. 转到Google API 控制台并选择您的 App Engine 项目。如果您没有看到您的 App Engine 项目列出,您需要先在 App Engine 管理工具中启用云集成(管理 > 应用程序设置 > 云集成 > 创建项目)

  2. 在 Google API 控制台中,现在转到服务并在那长长的列表中查找“Drive API”。打开它。

  3. 转到 Google API 控制台上的 API 访问部分,找到“简单 API 访问”API 密钥。(见下面的截图)

简单 API 访问 API 密钥

获取和安装 Python Drive API 客户端

  1. 下载 Python Drive API 客户端:https ://developers.google.com/api-client-library/python/start/installation#appengine

  2. 有关此 Python API 的文档:https ://google-api-client-libraries.appspot.com/documentation/drive/v2/python/latest/

使用 Python Drive API 客户端

要创建 Drive 服务对象,我使用以下命令:

import httplib2

def createDriveService():
    """Builds and returns a Drive service object authorized with the
       application's service account.
       Returns:
           Drive service object.
    """
    from oauth2client.appengine import AppAssertionCredentials
    from apiclient.discovery import build
    credentials = AppAssertionCredentials(scope='https://www.googleapis.com/auth/drive')
    http = httplib2.Http()
    http = credentials.authorize(http)
    return build('drive', 'v2', http=http, developerKey=API_KEY)

然后,您可以使用此服务对象来执行 Google Drive API 调用,例如,创建一个文件夹:

service = createDriveService()
res = {'title': foldername, 
       'mimeType': "application/vnd.google-apps.folder"}
service.files().insert(body=res).execute()

注意事项

我无法让 Drive API 在单元测试中工作,也无法在 dev_appserver 上工作。我总是收到我的凭据无效的错误。但是,它在真正的应用引擎服务器上运行良好。

于 2013-09-24T15:18:41.767 回答