Search in sources :

Example 6 with AccessToken

use of twitter4j.auth.AccessToken in project mobile-android by photo.

the class TwitterUtils method instantiateTwitter.

/**
     * Instantiate the twitter api instance
     * 
     * @param context
     * @return
     */
static Twitter instantiateTwitter(Context context) {
    Twitter twitter = null;
    // Get Access Token and persist it
    AccessToken a = TwitterUtils.getAccessToken(context);
    if (a != null) {
        // initialize Twitter4J
        twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(getConsumerKey(context), getConsumerSecret(context));
        twitter.setOAuthAccessToken(a);
    }
    return twitter;
}
Also used : AccessToken(twitter4j.auth.AccessToken) Twitter(twitter4j.Twitter) TwitterFactory(twitter4j.TwitterFactory)

Example 7 with AccessToken

use of twitter4j.auth.AccessToken in project twitter-2-weibo by rjyo.

the class UserServlet method handleRequest.

@Override
protected Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context ctx) {
    HttpServletRouter r = new HttpServletRouter(request);
    r.setPattern("/:id");
    HttpSession session = request.getSession(false);
    DBHelper helper = (DBHelper) request.getAttribute(Keys.REQUEST_DB_HELPER);
    // Service limit
    String uId = r.get(":id");
    if (!helper.isUser(uId) && helper.getUserCount() > 50) {
        return getTemplate("full.vm");
    }
    T2WUser user = helper.findOneByUser(uId);
    if (r.has(":id")) {
        log.info("Displaying user info for @" + uId);
        ctx.put("user_id", uId);
        ctx.put("user", helper.findOneByUser(uId));
        try {
            User weiboUser = (User) session.getAttribute(Keys.SESSION_WEIBO_USER);
            if (weiboUser == null) {
                Users um = new Users();
                weiboUser = um.showUserById(user.getWeiboUserId());
                session.setAttribute(Keys.SESSION_WEIBO_USER, weiboUser);
            }
            ctx.put("weibo_user", weiboUser.getScreenName());
            ctx.put("weibo_user_image", weiboUser.getProfileImageURL().toString());
            ctx.put("weibo_login", 1);
            // save weiboUser ID mapping
            helper.setWeiboId(user.getUserId(), weiboUser.getScreenName());
        } catch (Exception e) {
            // 401 = not logged in
            if (e instanceof WeiboException && ((WeiboException) e).getStatusCode() != 401) {
                e.printStackTrace();
            }
        }
        try {
            twitter4j.User twitterUser = (twitter4j.User) session.getAttribute(Keys.SESSION_TWITTER_USER);
            if (twitterUser == null) {
                TwitterFactory factory = new TwitterFactory();
                Twitter t = factory.getInstance();
                t.setOAuthAccessToken(new AccessToken(user.getTwitterToken(), user.getTwitterTokenSecret()));
                twitterUser = t.verifyCredentials();
                session.setAttribute(Keys.SESSION_TWITTER_USER, twitterUser);
            }
            ctx.put("twitter_user", twitterUser.getScreenName());
            ctx.put("twitter_user_image", twitterUser.getProfileImageURL().toString());
            ctx.put("twitter_login", 1);
        } catch (Exception e) {
            // 401 = not logged in
            if (e instanceof TwitterException && ((TwitterException) e).getStatusCode() != 401) {
                e.printStackTrace();
            }
        }
    }
    Object message = session.getAttribute(Keys.SESSION_MESSAGE);
    if (message != null) {
        ctx.put("message", message);
        session.removeAttribute(Keys.SESSION_MESSAGE);
    }
    Object prompt = session.getAttribute(Keys.SESSION_PROMPT_TWEET);
    if (prompt != null) {
        ctx.put("prompt", prompt);
        session.removeAttribute(Keys.SESSION_PROMPT_TWEET);
    }
    return getTemplate("user.vm");
}
Also used : T2WUser(h2weibo.model.T2WUser) User(weibo4j.model.User) HttpSession(javax.servlet.http.HttpSession) DBHelper(h2weibo.model.DBHelper) Twitter(twitter4j.Twitter) Users(weibo4j.Users) TwitterFactory(twitter4j.TwitterFactory) WeiboException(weibo4j.model.WeiboException) TwitterException(twitter4j.TwitterException) WeiboException(weibo4j.model.WeiboException) T2WUser(h2weibo.model.T2WUser) AccessToken(twitter4j.auth.AccessToken) HttpServletRouter(h2weibo.HttpServletRouter) TwitterException(twitter4j.TwitterException)

Example 8 with AccessToken

use of twitter4j.auth.AccessToken in project AndroidTwitter by lorensiuswlt.

the class TwitterSession method getAccessToken.

public AccessToken getAccessToken() {
    String token = sharedPref.getString(TWEET_AUTH_KEY, null);
    String tokenSecret = sharedPref.getString(TWEET_AUTH_SECRET_KEY, null);
    if (token != null && tokenSecret != null)
        return new AccessToken(token, tokenSecret);
    else
        return null;
}
Also used : AccessToken(twitter4j.auth.AccessToken)

Example 9 with AccessToken

use of twitter4j.auth.AccessToken in project twitter4j by yusuke.

the class DAOTest method getOAuthOuthorization.

private OAuthAuthorization getOAuthOuthorization(Configuration conf) {
    OAuthAuthorization oauth = new OAuthAuthorization(conf);
    oauth.setOAuthConsumer(desktopConsumerKey, desktopConsumerSecret);
    oauth.setOAuthAccessToken(new AccessToken(id1.accessToken, id1.accessTokenSecret));
    return oauth;
}
Also used : AccessToken(twitter4j.auth.AccessToken) OAuthAuthorization(twitter4j.auth.OAuthAuthorization)

Example 10 with AccessToken

use of twitter4j.auth.AccessToken in project twitter4j by yusuke.

the class GetAccessToken method main.

/**
     * Usage: java  twitter4j.examples.oauth.GetAccessToken [consumer key] [consumer secret]
     *
     * @param args message
     */
public static void main(String[] args) {
    File file = new File("twitter4j.properties");
    Properties prop = new Properties();
    InputStream is = null;
    OutputStream os = null;
    try {
        if (file.exists()) {
            is = new FileInputStream(file);
            prop.load(is);
        }
        if (args.length < 2) {
            if (null == prop.getProperty("oauth.consumerKey") && null == prop.getProperty("oauth.consumerSecret")) {
                // consumer key/secret are not set in twitter4j.properties
                System.out.println("Usage: java twitter4j.examples.oauth.GetAccessToken [consumer key] [consumer secret]");
                System.exit(-1);
            }
        } else {
            prop.setProperty("oauth.consumerKey", args[0]);
            prop.setProperty("oauth.consumerSecret", args[1]);
            os = new FileOutputStream("twitter4j.properties");
            prop.store(os, "twitter4j.properties");
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
        System.exit(-1);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ignore) {
            }
        }
        if (os != null) {
            try {
                os.close();
            } catch (IOException ignore) {
            }
        }
    }
    try {
        Twitter twitter = new TwitterFactory().getInstance();
        RequestToken requestToken = twitter.getOAuthRequestToken();
        System.out.println("Got request token.");
        System.out.println("Request token: " + requestToken.getToken());
        System.out.println("Request token secret: " + requestToken.getTokenSecret());
        AccessToken accessToken = null;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while (null == accessToken) {
            System.out.println("Open the following URL and grant access to your account:");
            System.out.println(requestToken.getAuthorizationURL());
            try {
                Desktop.getDesktop().browse(new URI(requestToken.getAuthorizationURL()));
            } catch (UnsupportedOperationException ignore) {
            } catch (IOException ignore) {
            } catch (URISyntaxException e) {
                throw new AssertionError(e);
            }
            System.out.print("Enter the PIN(if available) and hit enter after you granted access.[PIN]:");
            String pin = br.readLine();
            try {
                if (pin.length() > 0) {
                    accessToken = twitter.getOAuthAccessToken(requestToken, pin);
                } else {
                    accessToken = twitter.getOAuthAccessToken(requestToken);
                }
            } catch (TwitterException te) {
                if (401 == te.getStatusCode()) {
                    System.out.println("Unable to get the access token.");
                } else {
                    te.printStackTrace();
                }
            }
        }
        System.out.println("Got access token.");
        System.out.println("Access token: " + accessToken.getToken());
        System.out.println("Access token secret: " + accessToken.getTokenSecret());
        try {
            prop.setProperty("oauth.accessToken", accessToken.getToken());
            prop.setProperty("oauth.accessTokenSecret", accessToken.getTokenSecret());
            os = new FileOutputStream(file);
            prop.store(os, "twitter4j.properties");
            os.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
            System.exit(-1);
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException ignore) {
                }
            }
        }
        System.out.println("Successfully stored access token to " + file.getAbsolutePath() + ".");
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get accessToken: " + te.getMessage());
        System.exit(-1);
    } catch (IOException ioe) {
        ioe.printStackTrace();
        System.out.println("Failed to read the system input.");
        System.exit(-1);
    }
}
Also used : Twitter(twitter4j.Twitter) TwitterFactory(twitter4j.TwitterFactory) URISyntaxException(java.net.URISyntaxException) Properties(java.util.Properties) URI(java.net.URI) RequestToken(twitter4j.auth.RequestToken) AccessToken(twitter4j.auth.AccessToken) TwitterException(twitter4j.TwitterException)

Aggregations

AccessToken (twitter4j.auth.AccessToken)22 IOException (java.io.IOException)8 TwitterException (twitter4j.TwitterException)7 Twitter (twitter4j.Twitter)5 TwitterFactory (twitter4j.TwitterFactory)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 InetSocketAddress (java.net.InetSocketAddress)4 Socket (java.net.Socket)4 GeneralSecurityException (java.security.GeneralSecurityException)4 Calendar (java.util.Calendar)4 SSLContext (javax.net.ssl.SSLContext)4 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)4 DefaultHttpClientConnection (org.apache.http.impl.DefaultHttpClientConnection)4 BasicHttpEntityEnclosingRequest (org.apache.http.message.BasicHttpEntityEnclosingRequest)4 BasicHttpParams (org.apache.http.params.BasicHttpParams)4 HttpParams (org.apache.http.params.HttpParams)4 RequestToken (twitter4j.auth.RequestToken)4 T2WUser (h2weibo.model.T2WUser)3 File (java.io.File)3 KeyManagementException (java.security.KeyManagementException)3