发布推文并将图片上传到twitter使用c# [英] posting tweets and uploading pictures to twitterusing c#
问题描述
你好,我创建了一个ac#应用程序,在Twitter上的帐号上发布推文和图片,但我遇到了一个我似乎无法理解的错误。
如果有人帮我找出问题我会感谢谢谢!
这里是代码:
hello i created a c# application that posts tweets and pictures on an account on twitter but im stuck with an error i can't seem to understand.
if someone ca help identify the problem i would appreciate it thanks!
here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TweetSharp;
using Hammock;
namespace TweetNow
{
class TweetManager
{
static public TwitterService initiate()
{
String _consumerKey = "epG4Cd8W06r8pxlt05I8Hg";
String _consumerSecret = "aattB5375aOn0tfpWSj0rdNUiYCwIfsQzwLGG1yVGFE";
String _accessToken = "479270385-Nda8X27zzi5xBKfUioNm9U8qCOzOvakVSGkS4YJS";
String _accessTokenSecret = "8rSKr6ph6oRJ4pISjZDny2mMyn4ldX1AoSHwtzx4";
// Pass your credentials to the service
TwitterService service = new TwitterService(_consumerKey, _consumerSecret);
// Retrieve an OAuth Request Token
service.AuthenticateWith(_accessToken, _accessTokenSecret);
return service;
}
public static void sendTweet(String msg,String picname,TwitterService serv) {
RestRequest request = serv.PrepareEchoRequest();
request.Path = "uploadAndPost.xml";
request.AddFile("media", "smile", "Images/"+picname, "image/jpeg");
//request.AddField("key", "b9deee8ea5af8a6e1bac2aeb9d541c46");
request.AddField("key", "560adb8b7b328ae25ae3e7d3436de399");
request.AddField("message", "Emote with a Pic!");
RestClient client = new RestClient { Authority = "http://api.twitpic.com/", VersionPath = "2" };
RestResponse response = client.Request(request);
Console.WriteLine(response.Content);
String str = response.Content;
int first = str.IndexOf("<url>");
int last = str.LastIndexOf("</url>");
string str2 = str.Substring(first + 5, last - first - 5);
serv.SendTweet(msg+" "+str2+" .");
}
}
}
和错误我得到的是:
system.ArgumentOutOfRangeException:长度不能小于yero。
参数名称:长度
在System.String.InternalSubStringWithChecks (int32 startIndex,Int32 length,Boolean fAlwaysCopy)
TweetNow.TweetManager.sendTweet(String msg,String picname,TwitterService serv)
d:....
and the error i'm getting is :
system.ArgumentOutOfRangeException: Lenght cannot be less than yero.
Parameter name : length
at System.String.InternalSubStringWithChecks(int32 startIndex,Int32 length,Boolean fAlwaysCopy)
at TweetNow.TweetManager.sendTweet(String msg, String picname,TwitterService serv) in
d: ....
推荐答案
从错误中,我猜str2
是空白
或null
。
因为你不能发推文空白
值。 />
看到str2
之前的代码,我理解你在str中没有得到任何东西
在下面一行。
From the error, I guess thatstr2
is eitherblank
ornull
.
Because you can't tweetblank
values.
And see the codes beforestr2
, I apprehend that you are not getting anything instr
in the below line.
String str = response.Content;
请调试你的代码并找出你到底在哪里etting values。
Please debug your code and find out where exactly you are not getting values.
你是对的,我确实在你发布你的答案之前就发现了但是你猜对了
但是现在我面临另一个问题,即响应。内容是说Twitter拒绝了标题我该怎么做才能解决这个问题?任何想法?
并再次感谢帮助我:))
you were right,i actually found out before u post ur answer but you guessed it
but now im facing another problem,the response.Content is saying header rejected by twitter what can i do to solve this? any ideas?
and thanks again for helping me :))
这篇关于发布推文并将图片上传到twitter使用c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!