Java 当我想要获得twitter访问令牌时,这是怎么发生的?
输出以及stacktrace的输出为:Java 当我想要获得twitter访问令牌时,这是怎么发生的?,java,twitter,unauthorized,joauth,Java,Twitter,Unauthorized,Joauth,输出以及stacktrace的输出为: mZWtTPuWnBDiTuCgIclTID41vpES9juxTPOsoYpwB04 mZWtTPuWnBDiTuCgIclTID41vpES9juxTPOsoYpwB04 测试代码如下: 包com.auth import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URISyntaxExcep
mZWtTPuWnBDiTuCgIclTID41vpES9juxTPOsoYpwB04
mZWtTPuWnBDiTuCgIclTID41vpES9juxTPOsoYpwB04
测试代码如下: 包com.auth
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import net.oauth.consumer.OAuth1Consumer;
import net.oauth.exception.OAuthException;
import net.oauth.http.HttpClient;
import net.oauth.provider.OAuth1ServiceProvider;
import net.oauth.signature.OAuthSignature;
import net.oauth.signature.impl.OAuthHmacSha1Signature;
import net.oauth.token.oauth1.RequestToken;
import net.oauth.token.oauth1.AccessToken;
public class Test1OAuth1
{
public static void main(String[] args) throws OAuthException, IOException,
URISyntaxException
{
String client_id = "Consumer key";
String client_secret = "Consumer secret";
OAuth1Consumer consumer = new OAuth1Consumer(client_id, client_secret,
new OAuth1ServiceProvider(
"https://api.twitter.com/oauth/request_token",
"https://api.twitter.com/oauth/authorize",
"https://api.twitter.com/oauth/access_token"));
System.setProperty("proxySet", "true");
System.setProperty("proxyHost", "xxx.xxx.xxxx.xxx");
System.setProperty("proxyPort", "3128");
HttpClient client = new HttpProxyClient();
consumer.setClient(client);
OAuthSignature oAuthSignature = new OAuthHmacSha1Signature();
RequestToken requestToken = consumer.requestUnauthorizedToken(null,
"http://127.0.0.1:8080", null, oAuthSignature);
Map<String, String[]> map = new HashMap<String, String[]>();
String url = consumer
.createOAuthUserAuthorizationUrl(requestToken, map);
System.out.println(url);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String verify = br.readLine();
System.out.println(verify);
AccessToken accessToken = consumer.requestAccessToken(null,
requestToken, verify, oAuthSignature);
System.out.println(accessToken.getToken());
System.out.println(accessToken.getTokenSecret());
}
}
尝试将
Log4J
添加到应用程序中。在应用程序的根目录上添加log4j.properties
,然后运行。我调试了源代码并在类OAuth1Consumer.java中找到了它。我加上这3项声明。我可以获得访问令牌.oauthParameters.setOAuthVerifier(验证器);setOAuthToken(requestToken);setOAuthTokenSecret(requestTokenSecret);
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import net.oauth.consumer.OAuth1Consumer;
import net.oauth.exception.OAuthException;
import net.oauth.http.HttpClient;
import net.oauth.provider.OAuth1ServiceProvider;
import net.oauth.signature.OAuthSignature;
import net.oauth.signature.impl.OAuthHmacSha1Signature;
import net.oauth.token.oauth1.RequestToken;
import net.oauth.token.oauth1.AccessToken;
public class Test1OAuth1
{
public static void main(String[] args) throws OAuthException, IOException,
URISyntaxException
{
String client_id = "Consumer key";
String client_secret = "Consumer secret";
OAuth1Consumer consumer = new OAuth1Consumer(client_id, client_secret,
new OAuth1ServiceProvider(
"https://api.twitter.com/oauth/request_token",
"https://api.twitter.com/oauth/authorize",
"https://api.twitter.com/oauth/access_token"));
System.setProperty("proxySet", "true");
System.setProperty("proxyHost", "xxx.xxx.xxxx.xxx");
System.setProperty("proxyPort", "3128");
HttpClient client = new HttpProxyClient();
consumer.setClient(client);
OAuthSignature oAuthSignature = new OAuthHmacSha1Signature();
RequestToken requestToken = consumer.requestUnauthorizedToken(null,
"http://127.0.0.1:8080", null, oAuthSignature);
Map<String, String[]> map = new HashMap<String, String[]>();
String url = consumer
.createOAuthUserAuthorizationUrl(requestToken, map);
System.out.println(url);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String verify = br.readLine();
System.out.println(verify);
AccessToken accessToken = consumer.requestAccessToken(null,
requestToken, verify, oAuthSignature);
System.out.println(accessToken.getToken());
System.out.println(accessToken.getTokenSecret());
}
}
if (System.getProperty("proxySet").equals("true"))
{
if (null == proxyLocation)
{
proxyLocation = new InetSocketAddress(
System.getProperty("proxyHost"),
Integer.parseInt(System.getProperty("proxyPort")));
if (null == proxy)
{
proxy = new Proxy(Proxy.Type.HTTP, proxyLocation);
}
}
URLConnection uc = new URL(s).openConnection(proxy);
urlConnection = (HttpURLConnection) uc;
}
else
{
urlConnection = (HttpURLConnection) new URL(s).openConnection();
}