如何设置Google Calendar Java App

时间:2012-09-23 18:35:35

标签: java jar classpath google-calendar-api

我正在跟随Google一步一步configuration instructions,但出于某种原因,我找不到他们要求我导入的一些软件包。我的应用程序无法找到的软件包(或我的IDE抱怨的行):

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;

我似乎无法找到容纳这些的罐子。我的类路径包含:

//The Google Calendar Client API:
google-api-services-calendar-v3-rev16-1.8.0-beta.jar

//And of course the Google API Core:
commons-logging-1.1.1.jar
google-api-client-1.11.0-beta.jar
google-api-client-1.11.0-beta.jar.properties
google-api-client-android-1.11.0-beta.jar
google-api-client-android-1.11.0-beta.jar.properties
google-api-client-android2-1.11.0-beta.jar
google-api-client-android2-1.11.0-beta.jar.properties
google-api-client-appengine-1.11.0-beta.jar
google-api-client-java6-1.11.0-beta.jar
google-http-client-1.11.0-beta.jar
google-http-client-1.11.0-beta.jar.properties
google-http-client-android-1.11.0-beta.jar
google-http-client-android-1.11.0-beta.jar.properties
google-http-client-android2-1.11.0-beta.jar
google-http-client-android2-1.11.0-beta.jar.properties
google-http-client-android3-1.11.0-beta.jar
google-http-client-android3-1.11.0-beta.jar.properties
google-http-client-appengine-1.11.0-beta.jar
google-http-client-gson-1.11.0-beta.jar
google-http-client-gson-1.11.0-beta.jar.properties
google-http-client-jackson-1.11.0-beta.jar
google-http-client-jackson-1.11.0-beta.jar.properties
google-http-client-jackson2-1.11.0-beta.jar
google-http-client-jackson2-1.11.0-beta.jar.properties
google-oauth-client-1.11.0-beta.jar
google-oauth-client-1.11.0-beta.jar.properties
google-oauth-client-appengine-1.11.0-beta.jar
google-oauth-client-java6-1.11.0-beta.jar
google-oauth-client-jetty-1.11.0-beta.jar
google-oauth-client-servlet-1.11.0-beta.jar
gson-2.1.jar
gson-2.1.jar.properties
guava-11.0.1.jar
guava-11.0.1.jar.properties
httpclient-4.0.3.jar
httpcore-4.0.1.jar
jackson-core-2.0.5.jar
jackson-core-2.0.5.jar.properties
jackson-core-asl-1.9.9.jar
jackson-core-asl-1.9.9.jar.properties
jdo2-api-2.3-eb.jar
jetty-6.1.26.jar
jetty-util-6.1.26.jar
jsr305-1.3.9.jar
transaction-api-1.1.jar
xpp3-1.1.4c.jar

我不确定它是什么我错过了,但我需要这些库继续本教程。如果需要更多信息,我将很乐意提供。对于Google Calendar API,我是新手。任何帮助表示赞赏!谢谢!

1 个答案:

答案 0 :(得分:12)

很遗憾,截至目前,Google还没有更新the Java configuration source code。您不需要这些类,并且正如其他评论所指出的那样,它们已被弃用。

将“draft10”导入替换为:

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.services.calendar.CalendarScopes;

然后,通过以下方式替换授权码(来自评论“步骤1:授权 - >”):

GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                httpTransport, jsonFactory, clientId, clientSecret,
            Arrays.asList(CalendarScopes.CALENDAR)).setAccessType("online")
                .setApprovalPrompt("auto").build();

String url = flow.newAuthorizationUrl().setRedirectUri(redirectUrl).build();
System.out.println("Please open the following URL in your browser then type the authorization code:");

System.out.println("  " + url);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String code = br.readLine();

GoogleTokenResponse response = flow.newTokenRequest(code)
                .setRedirectUri(redirectUrl).execute();
GoogleCredential credential = new GoogleCredential()
                .setFromTokenResponse(response);

// Create a new authorized API client
Calendar service = new Calendar.Builder(httpTransport, jsonFactory,
                credential).build();

我遇到了同样的问题,发现the drive sample code是最新的。我猜对了,让它成功了。 “授权代码流程”描述为here