Example usage for twitter4j.auth AccessToken getUserId

List of usage examples for twitter4j.auth AccessToken getUserId

Introduction

In this page you can find the example usage for twitter4j.auth AccessToken getUserId.

Prototype


public long getUserId() 

Source Link

Usage

From source file:net.nokok.twitduke.core.io.AccessTokenPropertyWriter.java

License:Open Source License

@Override
public void write(AccessToken accessToken) {
    Properties properties = new Properties();
    properties.put(PropertyKey.TOKEN, accessToken.getToken());
    properties.put(PropertyKey.TOKEN_SECRET, accessToken.getTokenSecret());
    properties.put(PropertyKey.USER_ID, String.valueOf(accessToken.getUserId()));
    properties.put(PropertyKey.SCREEN_NAME, accessToken.getScreenName());
    try (java.io.Writer writer = (propertyWriter == null) ? new FileWriter(ACCESS_TOKEN) : propertyWriter) {
        properties.store(writer, null);/*  w ww . j  a  v  a  2s .  com*/
    } catch (IOException ex) {
        throw new UncheckedIOException(ex);
    }
}

From source file:net.nokok.twitduke.core.type.AccessTokenProperty.java

License:Open Source License

/**
 * ??????/*  www  .  ja v  a2  s  .c  o m*/
 *
 * @param accessToken
 */
public AccessTokenProperty(AccessToken accessToken) {
    this.accessToken = accessToken;
    properties = new Properties();
    properties.put(TOKEN_KEY, accessToken.getToken());
    properties.put(TOKEN_SECRET_KEY, accessToken.getTokenSecret());
    properties.put(SCREEN_NAME_KEY, Optional.ofNullable(accessToken.getScreenName()).orElse(""));
    properties.put(USER_ID_KEY, String.valueOf(accessToken.getUserId()));
}

From source file:nz.net.speakman.android.dreamintweets.preferences.DreamPreferences.java

License:Apache License

public void putAccessToken(AccessToken token) {
    getPrefs().edit().putString(KEY_ACCESS_TOKEN, token.getToken())
            .putString(KEY_ACCESS_TOKEN_SECRET, token.getTokenSecret())
            .putLong(KEY_ACCESS_TOKEN_USER_ID, token.getUserId()).commit();
}

From source file:org.addhen.smssync.presentation.view.ui.activity.OauthActivity.java

License:Open Source License

@Override
public void onSuccess(TwitterOAuthView view, AccessToken accessToken) {
    TwitterAuthToken authToken = new TwitterAuthToken(accessToken.getToken(), accessToken.getTokenSecret());
    // Persist user session so we can perform actions on protected resources
    App.getTwitterInstance().getSessionManager().setActiveSession(
            new TwitterSession(authToken, accessToken.getUserId(), accessToken.getScreenName()));
    finish();/*from   ww w. j a va 2s .  c  o  m*/
}

From source file:org.getlantern.firetweet.util.ContentValuesCreator.java

License:Open Source License

public static ContentValues createAccount(final Configuration conf, final AccessToken accessToken,
        final User user, final int authType, final int color, final String apiUrlFormat,
        final boolean sameOAuthSigningUrl, final boolean noVersionSuffix) {
    if (user == null || user.getId() <= 0 || accessToken == null || user.getId() != accessToken.getUserId())
        return null;
    final ContentValues values = new ContentValues();
    values.put(Accounts.OAUTH_TOKEN, accessToken.getToken());
    values.put(Accounts.OAUTH_TOKEN_SECRET, accessToken.getTokenSecret());
    values.put(Accounts.CONSUMER_KEY, conf.getOAuthConsumerKey());
    values.put(Accounts.CONSUMER_SECRET, conf.getOAuthConsumerSecret());
    values.put(Accounts.AUTH_TYPE, authType);
    values.put(Accounts.ACCOUNT_ID, user.getId());
    values.put(Accounts.SCREEN_NAME, user.getScreenName());
    values.put(Accounts.NAME, user.getName());
    values.put(Accounts.PROFILE_IMAGE_URL, user.getProfileImageUrlHttps());
    values.put(Accounts.PROFILE_BANNER_URL, user.getProfileBannerImageUrl());
    values.put(Accounts.COLOR, color);//from  www .  ja v  a2  s.  com
    values.put(Accounts.IS_ACTIVATED, 1);
    values.put(Accounts.API_URL_FORMAT, apiUrlFormat);
    values.put(Accounts.SAME_OAUTH_SIGNING_URL, sameOAuthSigningUrl);
    values.put(Accounts.NO_VERSION_SUFFIX, noVersionSuffix);
    return values;
}

From source file:org.mla.cbox.shibboleth.idp.authn.impl.ProcessRedirectFromTwitter.java

License:Apache License

/** {@inheritDoc} */
@Override//from  w w w. j  a  v  a 2s.  co  m
protected void doExecute(@Nonnull final ProfileRequestContext profileRequestContext,
        @Nonnull final AuthenticationContext authenticationContext) {

    /* Ensure we were passed the incoming HTTP request */
    final HttpServletRequest servletRequest = getHttpServletRequest();
    if (servletRequest == null) {
        log.debug("{} Profile action does not contain an HttpServletRequest", getLogPrefix());
        ActionSupport.buildEvent(profileRequestContext, AuthnEventIds.NO_CREDENTIALS);
        return;
    }

    /* Obtain the Twitter Oauth verifier from the query string */
    final String verifier = servletRequest.getParameter("oauth_verifier");
    if (verifier == null || verifier.isEmpty()) {
        log.debug("{} No Twitter Oauth verifier in request", getLogPrefix());
        ActionSupport.buildEvent(profileRequestContext, AuthnEventIds.NO_CREDENTIALS);
        return;
    }

    log.debug("{} Twitter returned verifier {}", getLogPrefix(), verifier);

    /* Query Twitter for the access token */
    AccessToken accessToken = null;
    try {
        accessToken = this.twitterContext.getTwitter().getOAuthAccessToken(twitterContext.getRequestToken(),
                verifier);
    } catch (TwitterException e) {
        log.warn("{} exception obtaining access token from Twitter: {}", getLogPrefix(), e.getMessage());
        ActionSupport.buildEvent(profileRequestContext, AuthnEventIds.NO_CREDENTIALS);
        return;
    }

    log.info("{} Login by '{}' succeeded", getLogPrefix(), accessToken.getUserId());

    /* Attach the access token to the Twitter context */
    this.twitterContext.setAccessToken(accessToken);

    /* Complete the authentication flow by building the authentication result */
    buildAuthenticationResult(profileRequestContext, authenticationContext);
    ActionSupport.buildProceedEvent(profileRequestContext);
}

From source file:org.mla.cbox.shibboleth.idp.authn.impl.TwitterIdPrincipal.java

License:Apache License

/**
 * Constructor from Twitter access token
 */// w w w  . j ava2 s. com
public TwitterIdPrincipal(AccessToken token) {
    this.userId = Long.toString(token.getUserId());
}

From source file:org.rhq.enterprise.server.plugins.alertMicroblog.MicroblogServerPluginComponent.java

License:Open Source License

private String storeAccessToken(AccessToken token) throws IOException {
    //use buffering
    String filePath = null;// w  w w.j  a  v  a  2  s.com
    if (this.context.getDataDirectory().exists() || this.context.getDataDirectory().mkdir()) {

        filePath = this.context.getDataDirectory().getAbsolutePath() + "/OAuthAccessToken_" + token.getUserId()
                + ".ser";

        // merge the PLugin Configuration to store the token file path reference.
        // this property will be user by Microblog AlertSender to load the accessToken from file system
        this.context.getPluginConfiguration().put(new PropertySimple("accessTokenFilePath", filePath));
        this.persistConfiguration(this.context.getPluginConfiguration());

        OutputStream file = new FileOutputStream(filePath);
        OutputStream buffer = new BufferedOutputStream(file);
        ObjectOutput output = new ObjectOutputStream(buffer);

        try {
            output.writeObject(token);
        } finally {
            output.close();

            log.info("AccessToken saved at " + filePath);
        }
    } else
        throw new IOException("AccessToken not stored!");

    return filePath;
}

From source file:ua.group06.presentation.UserLoginTwitter.java

/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request//from w w  w  .  ja  v a2 s. co  m
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Twitter twitter = twitterauth.getTwitter();
    RequestToken requestToken = twitterauth.getRequestToken();
    //RequestToken requestToken = (RequestToken) request.getSession().getAttribute("requestToken");
    String verifier = request.getParameter("oauth_verifier");
    System.err.println(twitter + " " + requestToken + " " + verifier);
    try {
        AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
        System.err.println(accessToken.getScreenName());
        User twitterUser = twitter.showUser(accessToken.getUserId());
        String fullName = twitterUser.getName();
        String fName = "";
        String lName = "";
        String[] names = fullName.split(" ");
        if (names.length == 1) {
            fName = names[0];
        } else if (names.length >= 2) {
            fName = names[0];
            lName = names[names.length - 1];
        }
        ExternalUser user = userService.authenticateOrCreate(accessToken.getScreenName(), "", fName, lName);

        if (user != null) {
            HttpSession session = request.getSession();
            session.setAttribute("user", user);
        }
        //request.getSession().removeAttribute("requestToken");
    } catch (TwitterException e) {
        throw new ServletException(e);
    }
    response.sendRedirect("homepage");
}