我正在开发 Twitter Rest 示例。在这个例子中,我希望调用Twitter REST API。当我运行以下程序时,我看到错误。你能告诉我这是什么问题吗?
public class JavaRestTweet {
static String consumerKeyStr = "RSvE3XXXXXXXXXXXX";
static String consumerSecretStr = "nKEWRoOcIXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
static String accessTokenStr = "1371430742-XXXXXXXXXXXXXXXXXXXXXXXXXX";
static String accessTokenSecretStr = "6ofNXMXXXXXXXXXXXXXXXXXXXXXXXXX";
public static void main(String[] args) throws Exception {
OAuthConsumer oAuthConsumer = new CommonsHttpOAuthConsumer(consumerKeyStr, consumerSecretStr);
oAuthConsumer.setTokenWithSecret(accessTokenStr, accessTokenSecretStr);
HttpPost httpPost = new HttpPost(
"http://api.twitter.com/1.1/statuses/update.json?status=Hello%20Twitter%20World.");
oAuthConsumer.sign(httpPost);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpPost);
int statusCode = httpResponse.getStatusLine().getStatusCode();
System.out.println(statusCode + ':' + httpResponse.getStatusLine().getReasonPhrase());
System.out.println(IOUtils.toString(httpResponse.getEntity().getContent()));
}
}
错误:
461 Forbidden
{"errors":[{"message":"SSL is required","code":92}]}
的pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- Logging -->
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>
</properties>
<dependencies>
<dependency>
<groupId>oauth.signpost</groupId>
<artifactId>signpost-core</artifactId>
<version>1.2.1.2</version>
</dependency>
<dependency>
<groupId>oauth.signpost</groupId>
<artifactId>signpost-commonshttp4</artifactId>
<version>1.2.1.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.3</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
答案 0 :(得分:1)
错误消息告诉您确切需要知道的内容:
需要SSL
问题似乎是您在代码中使用http://而不是https://:
http://api.twitter.com/1.1/statuses/update.json
答案 1 :(得分:1)
var oauth_consumer_key = "your consumer key;
var oauth_consumer_secret = "secret key";
if (Request["oauth_token"] == null)
{
OAuthTokenResponse reqToken = OAuthUtility.GetRequestToken(
oauth_consumer_key,
oauth_consumer_secret,
Request.Url.AbsoluteUri);
Response.Redirect(string.Format("https://twitter.com/oauth/authorize?oauth_token={0}",
reqToken.Token));
}
else
{
string requestToken = Request["oauth_token"].ToString();
string pin = Request["oauth_verifier"].ToString();
var tokens = OAuthUtility.GetAccessToken(
oauth_consumer_key,
oauth_consumer_secret,
requestToken,
pin);
OAuthTokens accesstoken = new OAuthTokens()
{
AccessToken = tokens.Token,
AccessTokenSecret = tokens.TokenSecret,
ConsumerKey = oauth_consumer_key,
ConsumerSecret = oauth_consumer_secret
};
//uncomment for get likes and retweet count
//var response = TwitterStatus.Show(accesstoken,Convert.ToDecimal(post id), new StatusUpdateOptions() { UseSSL = true, APIBaseAddress = "https://api.twitter.com/1.1/" });
TwitterResponse<TwitterStatus> response = TwitterStatus.Update(accesstoken, "Posting This Tweet From Asp.Net Website using REST API", new StatusUpdateOptions() { UseSSL = true, APIBaseAddress = "https://api.twitter.com/1.1/" });
var obj = JObject.Parse(response.Content.ToString());
var id = (string)obj["id"].ToString();
}