My favorites
▼
|
Sign in
withgod
withgod's public codes
Project Home
Downloads
Wiki
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
twitter
/
YFrogSample.java
r36
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package sample;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationContext;
import twitter4j.http.AccessToken;
import twitter4j.http.OAuthAuthorization;
public class YFrogSample {
public static void main(String[] args) throws Exception{
if (args.length != 2 || !(new File(args[1])).exists()) {
System.err.println("no parameter/invalid filepath");
System.exit(1);
}
Configuration conf = ConfigurationContext.getInstance();
System.out.println(conf);
OAuthAuthorization oauth = new OAuthAuthorization(conf, conf.getOAuthConsumerKey(), conf.getOAuthConsumerSecret(),
new AccessToken(conf.getOAuthAccessToken(), conf.getOAuthAccessTokenSecret()));
YFHttpURLConnection yfCon = new YFHttpURLConnection();
oauth.setAuthorizationHeader("GET", "https://api.twitter.com/1/account/verify_credentials.xml",null, yfCon);
String signedURL = yfCon.authorizationHeader;
System.out.println("signedURL" + signedURL);
signedURL = signedURL.replaceFirst("^OAuth ", "");
String[] signedURLs = signedURL.split(",");
String verify_url = "https://api.twitter.com/1/account/verify_credentials.xml?";
System.out.println("signed parameters");
for (int i = 0; i < signedURLs.length; i++) {
String tmp = signedURLs[i].replaceAll("\"", "");
String[] kv = tmp.split("=");
System.out.println("\t" + i + ":" + kv[0] + "/" + URLDecoder.decode(kv[1], "UTF-8"));
verify_url += kv[0] + "=" + URLDecoder.decode(kv[1], "UTF-8") + "&";
}
verify_url = verify_url.replaceFirst("&$", "");
System.out.println("signed url rebuild [" + verify_url + "]");
HttpClient client = new HttpClient();
PostMethod post = new PostMethod("http://yfrog.com/api/upload");
File f = new File(args[1]);
Part[] parts = {
new StringPart("auth", "oauth"),
new StringPart("verify_url", verify_url),
new StringPart("username", args[0]),
new StringPart("password", ""),
new FilePart("media", f)
};
post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
int status;
try {
status = client.executeMethod(post);
if (status == HttpStatus.SC_OK) {
System.out.println("post complete");
System.out.println(post.getResponseBodyAsString(1024*2));
} else {
System.err.println("post failure");
System.err.println(HttpStatus.getStatusText(status));
}
} catch(Exception e) {
e.printStackTrace();
} finally {
post.releaseConnection();
}
}
static class YFHttpURLConnection extends HttpURLConnection {
public String authorizationHeader = null;
protected YFHttpURLConnection(URL u) {
super(u);
}
public YFHttpURLConnection() {
super(null);
}
public void addRequestProperty(String key, String value){
if("Authorization".equals(key)){
System.out.println("catch Authorization header [" + value + "]");
authorizationHeader = value;
}
}
@Override
public void disconnect() {
}
@Override
public boolean usingProxy() {
return false;
}
@Override
public void connect() throws IOException {
}
}
}
Show details
Hide details
Change log
r32
by nocontents on Feb 11, 2010
Diff
yfrog oauth post sample.
Go to:
/trunk/twitter
/trunk/twitter/YFrogSample.java
Older revisions
All revisions of this file
File info
Size: 3693 bytes, 108 lines
View raw file
Powered by
Google Project Hosting