summaryrefslogtreecommitdiff
path: root/src/Chapter2/openauthentication/OAuthExample.java
blob: 34ca3a1e0e72ee099e612a24429a6ab0f612ef53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* TweetTracker. Copyright (c) Arizona Board of Regents on behalf of Arizona State University
 * @author shamanth
 */
package Chapter2.openauthentication;

import Chapter2.support.OAuthTokenSecret;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;
import utils.Configuration;

public class OAuthExample
{        
    public OAuthTokenSecret GetUserAccessKeySecret()
    {
        try {
            //consumer key for Twitter Data Analytics application
            if(Configuration.CONSUMER_KEY.isEmpty())
            {
                System.out.println("Register an application and copy the consumer key into the configuration file.");
                return null;
            }
            if(Configuration.CONSUMER_SECRET.isEmpty())
            {
                System.out.println("Register an application and copy the consumer secret into the configuration file.");
                return null;
            }
            OAuthConsumer consumer = new CommonsHttpOAuthConsumer(Configuration.CONSUMER_KEY,Configuration.CONSUMER_SECRET);
            OAuthProvider provider = new DefaultOAuthProvider(Configuration.REQUEST_TOKEN_URL, Configuration.ACCESS_TOKEN_URL, Configuration.AUTHORIZE_URL);
            String authUrl = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);
            System.out.println("Now visit:\n" + authUrl + "\n and grant this app authorization");
            System.out.println("Enter the PIN code and hit ENTER when you're done:");
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String pin = br.readLine();
            System.out.println("Fetching access token from Twitter");
            provider.retrieveAccessToken(consumer,pin);
            String accesstoken = consumer.getToken();
            String accesssecret  = consumer.getTokenSecret();
            OAuthTokenSecret tokensecret = new OAuthTokenSecret(accesstoken,accesssecret);
            return tokensecret;
        } catch (OAuthNotAuthorizedException ex) {
                ex.printStackTrace();
        } catch (OAuthMessageSignerException ex) {
                ex.printStackTrace();
        } catch (OAuthExpectationFailedException ex) {
                ex.printStackTrace();
        } catch (OAuthCommunicationException ex) {
                ex.printStackTrace();
        } catch(IOException ex)
        {
            ex.printStackTrace();
        }
        return null;
    }

    public static OAuthTokenSecret DEBUGUserAccessSecret()
    {
        String accesstoken = "1262619914-tcCPB1SyXy3BMuui9OAhprcPmqg3z2csSjDSCNY";
        String accesssecret = "cXXO0qFLBjLXGtE97pnf5Vv1RZGxZ2FZ97wCYiaVU";
        OAuthTokenSecret tokensecret = new OAuthTokenSecret(accesstoken,accesssecret);
        return tokensecret;
    }

    public static void main(String[] args)
    {
        OAuthExample aue = new OAuthExample();
        OAuthTokenSecret tokensecret = aue.GetUserAccessKeySecret();
        System.out.println(tokensecret.toString());
    }
}