我想通过 C# 将图像发布到我的 Twitter 帐户。我可以获得访问令牌代码,一切都很好,但我调查了一个 PHP 代码
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => OAUTH_CONSUMER_KEY,
'consumer_secret' => OAUTH_CONSUMER_SECRET,
'user_token' => $oauth_token,
'user_secret' => $oauth_token_secret,
));
$image = "{$_FILES['image']['tmp_name']};type={$_FILES['image']['type']};filename={$_FILES['image']['name']}";
$code = $tmhOAuth->request('POST', 'https://upload.twitter.com/1/statuses/update_with_media.json',
array(
'media[]' => "@{$image}",
'status' => " " . $status, //A space is needed because twitter b0rks if first char is an @
'lat' => $lat,
'long' => $long,
),
true, // use auth
true // multipart
在 PHP 代码中,OAuth
该类有一个请求方法。在 C# 方面,我使用了类中没有任何请求方法的 Twitterizer 库OAuth
。然后我用Webclient
了代替请求方法。但我需要一些凭据来发布数据。但我不知道我使用什么/为什么使用用户名和密码。实际上,我不想使用任何凭据。我可以使用什么来代替凭据?
第二个问题是,我总是得到一个授权错误(401)这里是代码
OAuthTokenResponse responseToken = OAuthUtility.GetAccessToken(ConsumerKey, ConsumerSecret, oauth_token, oauth_verifier);
OAuthTokens accessToken = new OAuthTokens();
accessToken.AccessToken = responseToken.Token;
accessToken.AccessTokenSecret = responseToken.TokenSecret;
accessToken.ConsumerKey = ConsumerKey;
accessToken.ConsumerSecret = ConsumerSecret;
TwitterResponse<TwitterUser> twitterResponse = TwitterAccount.VerifyCredentials(accessToken);
System.Net.ServicePointManager.Expect100Continue = false;
if (twitterResponse.Result != RequestResult.Unauthorized)
{
try
{
string URL = "https://upload.twitter.com/1/statuses/update_with_media.json";
WebClient client = new WebClient();
client.Credentials = new System.Net.NetworkCredential(uName, pass);
NameValueCollection postData = new NameValueCollection();
postData.Add("status", status);
postData.Add("media[]", Encoding.ASCII.GetString(bytesOfImage));
byte[] b = client.UploadValues(URL, "POST", postData); // 401 error.
}
catch (Exception e)
{
return e.Message;
}
那么我的代码中的问题在哪里?