Ejemplo n.º 1
0
 @Test
 public void test() {
   logger.debug("Running '{}'...", name.getMethodName());
   con = usersConnectionRepository.createConnectionRepository("GeraldXv");
   Connection<Twitter> twitter = con.findPrimaryConnection(Twitter.class);
   twitterApi = twitter.getApi();
   Connection<Google> google = con.findPrimaryConnection(Google.class);
   googleApi = google.getApi();
   System.out.println(twitterApi.friendOperations().getFriends().get(0).getName());
   // System.out.println(googleApi.personOperations().getPerson("110377639084744464746").get);
 }
 @Test
 public void updateConnectionAccessFields() {
   insertFacebookConnection();
   Connection<TestFacebookApi> facebook =
       connectionRepository.getPrimaryConnection(TestFacebookApi.class);
   assertEquals("234567890", facebook.getApi().getAccessToken());
   facebook.refresh();
   connectionRepository.updateConnection(facebook);
   Connection<TestFacebookApi> facebook2 =
       connectionRepository.getPrimaryConnection(TestFacebookApi.class);
   assertEquals("765432109", facebook2.getApi().getAccessToken());
   ConnectionData data = facebook.createData();
   assertEquals("654321098", data.getRefreshToken());
 }
 @RequestMapping(value = "/twitter", method = RequestMethod.GET)
 public String home(Principal currentUser, Model model) {
   Connection<Twitter> connection = connectionRepository.findPrimaryConnection(Twitter.class);
   if (connection == null) {
     return "redirect:/connect/twitter";
   }
   model.addAttribute("profile", connection.getApi().userOperations().getUserProfile());
   return "twitter/profile";
 }
 private void assertTwitterConnection(Connection<TestTwitterApi> twitter) {
   assertEquals(new ConnectionKey("twitter", "1"), twitter.getKey());
   assertEquals("@kdonald", twitter.getDisplayName());
   assertEquals("http://twitter.com/kdonald", twitter.getProfileUrl());
   assertEquals("http://twitter.com/kdonald/picture", twitter.getImageUrl());
   TestTwitterApi twitterApi = twitter.getApi();
   assertEquals("123456789", twitterApi.getAccessToken());
   assertEquals("987654321", twitterApi.getSecret());
   twitter.sync();
   assertEquals("http://twitter.com/kdonald/a_new_picture", twitter.getImageUrl());
 }
 private void assertFacebookConnection(Connection<TestFacebookApi> facebook) {
   assertEquals(new ConnectionKey("facebook", "9"), facebook.getKey());
   assertEquals(null, facebook.getDisplayName());
   assertEquals(null, facebook.getProfileUrl());
   assertEquals(null, facebook.getImageUrl());
   TestFacebookApi facebookApi = facebook.getApi();
   assertEquals("234567890", facebookApi.getAccessToken());
   facebook.sync();
   assertEquals("Keith Donald", facebook.getDisplayName());
   assertEquals("http://facebook.com/keith.donald", facebook.getProfileUrl());
   assertEquals("http://facebook.com/keith.donald/picture", facebook.getImageUrl());
 }
 @Override
 @RequestMapping(value = "/{providerId}", method = RequestMethod.GET, params = "oauth_token")
 public RedirectView oauth1Callback(@PathVariable String providerId, NativeWebRequest request) {
   connectSupport = new ConnectSupport(sessionStrategy);
   try {
     OAuth1ConnectionFactory<?> connectionFactory =
         (OAuth1ConnectionFactory<?>) connectionFactoryLocator.getConnectionFactory(providerId);
     Connection<?> connection = connectSupport.completeConnection(connectionFactory, request);
     Serializable userId = null;
     switch (providerId) {
       case "google":
         google = (Google) connection.getApi();
         userId = ApiBindingUtils.getId(google);
         break;
       case "facebook":
         facebook = (Facebook) connection.getApi();
         userId = ApiBindingUtils.getId(facebook);
         break;
       case "twitter":
         twitter = (Twitter) connection.getApi();
         userId = ApiBindingUtils.getId(twitter);
         break;
       default:
         break;
     }
     SecurityContextHolder.getContext()
         .setAuthentication(new SocialAuthenticationToken(connection, userId, null, null));
     addConnection(connection, connectionFactory, request);
   } catch (Exception e) {
     sessionStrategy.setAttribute(request, PROVIDER_ERROR_ATTRIBUTE, e);
     logger.warn(
         "Exception while handling OAuth1 callback ("
             + e.getMessage()
             + "). Redirecting to "
             + providerId
             + " connection status page.");
   }
   return connectionStatusRedirect(providerId, request);
 }
 private void assertNewConnection(Connection<TestFacebookApi> connection) {
   assertEquals("facebook", connection.getKey().getProviderId());
   assertEquals("9", connection.getKey().getProviderUserId());
   assertEquals("Keith Donald", connection.getDisplayName());
   assertEquals("http://facebook.com/keith.donald", connection.getProfileUrl());
   assertEquals("http://facebook.com/keith.donald/picture", connection.getImageUrl());
   assertTrue(connection.test());
   TestFacebookApi api = connection.getApi();
   assertNotNull(api);
   assertEquals("123456789", api.getAccessToken());
   assertEquals("123456789", connection.createData().getAccessToken());
   assertEquals("987654321", connection.createData().getRefreshToken());
 }
    public LinkedIn getApi() {
      if (logger.isDebugEnabled()) {
        logger.debug("Getting API binding instance for Facebook");
      }

      Connection<LinkedIn> connection =
          usersConnectionRepository
              .createConnectionRepository(userIdSource.getUserId())
              .findPrimaryConnection(LinkedIn.class);
      if (logger.isDebugEnabled() && connection == null) {
        logger.debug("No current connection; Returning default FacebookTemplate instance.");
      }
      return connection != null ? connection.getApi() : null;
    }