2012-01-12 79 views
0

我想使用Google Calendar API来拉取日历,事件等。我有android中的AccountManager类中的认证令牌。因此,如果用户使用他的谷歌账户登录移动设备,那么我可以轻松地检索与他相关的身份验证令牌。 后thati做到以下几点:使用Google Calendar API访问用户的日历列表时出现NullPointerException

public Calendar useCalendarAPI(final String accessToken) { 

try{ 

     HttpTransport transport = new NetHttpTransport(); 
     AccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(accessToken); 
     Calendar.Builder builder = Calendar.builder(transport, new JacksonFactory()); 
     builder.setApplicationName("My App Name"); 
     builder.setHttpRequestInitializer(accessProtectedResource); 
     JsonHttpRequestInitializer json = new JsonHttpRequestInitializer() { 
      public void initialize(JsonHttpRequest request) { 
       CalendarRequest calRequest = (CalendarRequest) request; 
       calRequest.setKey(Common.API_KEY); 
       calRequest.setOauthToken(accessToken); 
      } 
      }; 
     builder.setJsonHttpRequestInitializer(json); 

     Calendar calendarService= builder.build(); 

     if(calendarService!= null) 
     { 
      return calendarService; 
     } 
     else 
      return null; 
     }catch (Exception e) { 
     // TODO: handle exception 
      e.getMessage(); 
    } 

在此之后我收到称为“calendarService”日历对象。

public void getCalendarsList() 
{ 
     try  
     { 
      CalendarList calendarList = calendarService.calendarList().list().execute(); 

      while (true) { 
       for (CalendarListEntry calendarListEntry : calendarList.getItems()) { 
        System.out.println(calendarListEntry.getSummary()); 
       } 
       String pageToken = calendarList.getNextPageToken(); 
       if (pageToken != null && !pageToken.equalsIgnoreCase("")) { 
        calendarList = calendarService.calendarList().list().setPageToken(pageToken).execute(); 

       } else { 
        break; 
       } 
      } 

     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
} 

这是从Google Calendar API文档复制的。 检查此链接here
但我在下面一行

CalendarList calendarList = calendarService.calendarList().list().execute(); 

请建议的东西越来越空指针异常。

感谢

+0

我通过谷歌日历的私人API.Thanks实现日历整合 – Vansi 2012-01-31 07:15:09

回答

0

嘿我有使用你提到过相同的日历文件做这个小工作范例。如果您可以将堆栈跟踪与代码一起使用,它也会有所帮助。

public static void main(String[] args) throws IOException { 
    HttpTransport httpTransport = new NetHttpTransport(); 
    JacksonFactory jsonFactory = new JacksonFactory(); 
    String clientId = "CLIENT ID"; 
    String clientSecret = "CLIENT SECRET"; 
    String redirectUrl = "urn:ietf:wg:oauth:2.0:oob"; 
    String scope = "https://www.googleapis.com/auth/calendar"; 
    String authorizationUrl = new GoogleAuthorizationRequestUrl(clientId, redirectUrl, scope) 
    .build(); 
    System.out.println("Go to the following link in your browser:"); 
    System.out.println(authorizationUrl); 
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
    System.out.println("What is the authorization code?"); 
    String code = in.readLine(); 
    AccessTokenResponse response = new GoogleAuthorizationCodeGrant(httpTransport, jsonFactory, 
     clientId, clientSecret, code, redirectUrl).execute(); 
    GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(
     response.accessToken, httpTransport, jsonFactory, clientId, clientSecret, 
     response.refreshToken); 

    Calendar calendarService = Calendar.builder(httpTransport, jsonFactory) 
     .setApplicationName("YOUR_APPLICATION_NAME") 
     .setHttpRequestInitializer(accessProtectedResource) 
     .build(); 
    try  
    { 
     com.google.api.services.calendar.model.CalendarList calendarList = calendarService.calendarList().list().execute(); 

     while (true) { 
     for (CalendarListEntry calendarListEntry : calendarList.getItems()) { 
      System.out.println(calendarListEntry.getSummary()); 
     } 
     String pageToken = calendarList.getNextPageToken(); 
     if (pageToken != null && !pageToken.equalsIgnoreCase("")) { 
      calendarList = calendarService.calendarList().list().setPageToken(pageToken).execute(); 

     } else { 
      break; 
     } 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 
0

看起来你需要通过calendarService作为参数传递给getCalendarsList()功能

相关问题