【问题标题】:Access Google Drive API with refresh token, no browser, 401 error使用刷新令牌访问 Google Drive API,没有浏览器,401 错误
【发布时间】:2015-02-18 23:05:16
【问题描述】:

我正在尝试使用从 OAuth Playground 发出的刷新令牌访问 GoogleDrive API(我自己的空调),如下所示。我正在使用我的刷新令牌和访问令牌进行离线访问,但我得到了未经授权的 401。我的代码是基于参考 belwoand google-api javadocs 推荐的构建离线请求

我不想使用浏览器,我希望从服务器端应用程序运行它以上传到我自己的空调。

ref: http://stackoverflow.com/questions/10533203/fetching-access-token-from-refresh-token-using-java

代码:

public static void main(String[] args) throws IOException {
        HttpTransport TRANSPORT = new NetHttpTransport();
        JsonFactory JSON_FACTORY = new JacksonFactory();

        String refreshTokenString="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; //from console

        String accessTokenString ="yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"; //from console

        GoogleCredential credential = createCredentialWithRefreshToken(
            TRANSPORT, JSON_FACTORY, new TokenResponse().setRefreshToken(refreshTokenString));
    credential.setAccessToken("accessTokenString");


    //exeucute HTTP request for offline accessTokenString

    // Execute HTTP GET request to revoke current token.
    HttpResponse response = TRANSPORT.createRequestFactory()
            .buildGetRequest(new GenericUrl(
                    String.format(
                            "https://www.googleapis.com/drive/v2/changes",
                            credential.getAccessToken()))).execute();

    System.out.println("RESPONSE: " +response);

}

 public static GoogleCredential createCredentialWithRefreshToken(HttpTransport transport,
                                                                    JsonFactory jsonFactory, TokenResponse tokenResponse) {

    HttpTransport TRANSPORT = new NetHttpTransport();
    JsonFactory JSON_FACTORY = new JacksonFactory();
    return new GoogleCredential.Builder().setTransport(transport)
            .setJsonFactory(JSON_FACTORY)
            .setClientSecrets(CLIENT_ID, CLIENT_SECRET)
            .build()
            .setFromTokenResponse(tokenResponse);
}

还有错误:

    {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
 }
}

我正在使用有效的刷新令牌和访问令牌(从操场控制台工作),但从 Java 应用程序运行时,我在此处获得登录要求 401。我需要这个用于从服务器直接备份到云端硬盘的日常备份。

【问题讨论】:

    标签: oauth-2.0 google-api google-drive-api google-oauth


    【解决方案1】:

    您不能在应用程序中使用来自 Playgorund 的访问或刷新令牌。这些由谷歌发行的代币只能在操场上使用。您获得的令牌与客户端 ID 和客户端密码相关联。显然,您的 CLIENT_ID 和 CLIENT_SECRET 与 Google Playground 应用程序使用的不同。

    您确实需要您的应用程序来请求这些令牌,因此您第一次需要一个浏览器,以便用户可以接受您的应用程序(不是 Google Playground)正在将您的信息存储在 google 中。这称为“用户同意”,在此页面的小图表中显示:Using OAuth 2.0 for Web Server Applications)

    然后,当您拥有应用程序的刷新令牌时。您不再需要浏览器来进行日常备份。 您可以将代码与您的应用程序的有效刷新令牌一起使用,如您的帖子中所述。

    【讨论】:

      【解决方案2】:

      我能否仅使用刷新令牌进行访问,因为它不会过期?

      没有。您必须将刷新令牌转换为访问令牌。

      【讨论】:

      猜你喜欢
      • 2020-12-11
      • 2019-02-16
      • 2019-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多