实施谷歌硬盘的服务器端授权的问题

时间:2013-06-12 05:02:46

标签: java google-app-engine google-drive-api

我们希望让我们的GAE for java应用程序从谷歌驱动器中读取文档以满足应用程序的特定要求。

对于同样的情况,我试图引用https://developers.google.com/drive/auth/web-server

但是,我无法理解代码流。没有提到像从哪里获得下面代码中提到的authorizationCode:

 public static Credential getCredentials(String authorizationCode, String state)
      throws CodeExchangeException, NoRefreshTokenException, IOException {
    String emailAddress = "";
    try {
      Credential credentials = exchangeCode(authorizationCode);
      Userinfo userInfo = getUserInfo(credentials);
      String userId = userInfo.getId();
      emailAddress = userInfo.getEmail();
      if (credentials.getRefreshToken() != null) {
        storeCredentials(userId, credentials);
        return credentials;
      } else {
        credentials = getStoredCredentials(userId);
        if (credentials != null && credentials.getRefreshToken() != null) {
          return credentials;
        }
      }
    } catch (CodeExchangeException e) {

gae for java和google drive之间集成的任何工作示例?

1 个答案:

答案 0 :(得分:1)

如果您尝试进行离线访问,首先您必须获得可以兑换访问令牌和刷新令牌的授权码。

1)从Google APIs console

启用Google云端硬盘服务

2)为web applications

创建客户端ID

然后,获取授权码,例如:

https://accounts.google.com/o/oauth2/auth?access_type=offline
&approval_prompt=auto
&client_id=[your id]
&redirect_uri=[url]
&response_type=code
&scope=[access scopes]
&state=/profile

之后,您可以调用exchangeCode(),其参数将是您之前检索过的授权代码。