【问题标题】:how to configure an authorized service object using Google OAuth 2.0如何使用 Google OAuth 2.0 配置授权服务对象
【发布时间】:2012-11-14 22:51:02
【问题描述】:

在以下链接中,有代码应该使用 OAuth 2.0 访问 Google Calendar api。不幸的是,它使用了 Draft 10 客户端库,该库显然已被弃用。

https://developers.google.com/google-apps/calendar/instantiate

最新的客户端库是 google-api-java-client-1.12.0-beta。据我所知,自 Draft 10 客户端库以来,情况发生了很大变化,我不知道如何为当前的客户端库重写此代码。

不推荐使用的代码如下所示。

import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
import   com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessTokenRequest.GoogleAuthorizationCodeGrant;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAuthorizationRequestUrl;

import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;

import com.google.api.services.calendar.Calendar;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

...

public void setUp() throws IOException {
    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();

    // The clientId and clientSecret are copied from the API Access tab on
    // the Google APIs Console
    String clientId = "YOUR_CLIENT_ID";
    String clientSecret = "YOUR_CLIENT_SECRET";

    // Or your redirect URL for web based applications.
    String redirectUrl = "urn:ietf:wg:oauth:2.0:oob";
    String scope = "https://www.googleapis.com/auth/calendar";

    // Step 1: Authorize -->
    String authorizationUrl = new GoogleAuthorizationRequestUrl(clientId, redirectUrl,   scope)
    .build();

    // Point or redirect your user to the authorizationUrl.
    System.out.println("Go to the following link in your browser:");
    System.out.println(authorizationUrl);

    // Read the authorization code from the standard input stream.
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("What is the authorization code?");
    String code = in.readLine();
    // End of Step 1 <--

   // Step 2: Exchange -->
    AccessTokenResponse response = new GoogleAuthorizationCodeGrant(httpTransport,     jsonFactory,
    clientId, clientSecret, code, redirectUrl).execute();
   // End of Step 2 <--

  GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(
    response.accessToken, httpTransport, jsonFactory, clientId, clientSecret,
    response.refreshToken);

  Calendar service = new Calendar(httpTransport, accessProtectedResource, jsonFactory);
  service.setApplicationName("YOUR_APPLICATION_NAME");
   ...
}
...

谁能告诉如何重写此代码以使其适用于当前的客户端库?

【问题讨论】:

    标签: oauth-2.0 google-calendar-api


    【解决方案1】:

    您可以查看最新的 Google Drive API 文档:

    https://developers.google.com/drive/credentials

    那么用日历范围替换 Drive 范围并为服务实例化正确的 Calendar 类而不是 Drive 服务对象应该不难。

    如果这种情况再次发生,最好直接查看 Google APIs Client Library for Java 的网站,以确保您能找到最新版本的代码示例。您可以查看它的wiki about auth,也可以查看the sample apps,他们确保正在编译和使用最新版本的库(有时很难让所有文档保持最新)

    【讨论】:

    • 感谢 Nivco 的回复,你给了我很多学习和思考的信息。
    猜你喜欢
    • 2016-08-12
    • 2013-03-16
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 2015-01-15
    • 2014-09-08
    • 2012-11-12
    • 2012-10-01
    相关资源
    最近更新 更多