Ich versuche, eine unglaublich einfache C#-Konsolenanwendung zu schreiben, die in meinem Namen nur einen einzigen Tweet auf meinem Twitter postet.
Ich habe ein Entwicklerkonto und lese die Referenz, einschließlich der Teile, die keinen Sinn ergeben und derjenigen, die sich widersprechen. Ich habe ältere Fragen und Twitter-Foren durchgesehen und egal, was ich versuche, ich kann mich nie authentifizieren.
Mein Ziel ist es, NICHT zu einem Webbrowser umgeleitet zu werden, um mich manuell anzumelden Konto, aber verwenden Sie zur Authentifizierung die Schlüssel und Geheimnisse des Entwicklerkontos.
Ich habe alles Folgende ausprobiert:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the Tweet ID: ");
string tweetID = Console.ReadLine(); // no error checking here!
// convenient to load keys and tokens from a config file for testing
// edit .env.sample (and rename to .env) to add your keys and tokens (no quotation marks)
DotNetEnv.Env.Load();
string CONSUMER_KEY = "";
string CONSUMER_TOKEN = "";
string ACCESS_TOKEN = "";
string ACCESS_TOKEN_SECRET = "";
// this is the endpoint we will be calling
StringBuilder apiPath = new StringBuilder("https://api.twitter.com");
apiPath.Append("/2/tweets");
apiPath.AppendFormat("?ids={0}", tweetID);
apiPath.Append("&tweet.fields=attachments,author_id,context_annotations,created_at,entities,geo,id,in_reply_to_user_id,lang,possibly_sensitive,public_metrics,referenced_tweets,source,text,withheld");
apiPath.Append("&media.fields=duration_ms,height,media_key,preview_image_url,type,url,width");
apiPath.Append("&expansions=attachments.poll_ids,attachments.media_keys,author_id,geo.place_id,in_reply_to_user_id,referenced_tweets.id");
apiPath.Append("&poll.fields=duration_minutes,end_datetime,id,options,voting_status");
apiPath.Append("&place.fields=contained_within,country,country_code,full_name,geo,id,name,place_type");
apiPath.Append("&user.fields=created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified,withheld");
string REQUEST_URL = apiPath.ToString();
// if you would like to compare to v1.1 then this alternative REQUEST_URL does that for Tweet ID 20
// string REQUEST_URL = "https://api.twitter.com/labs/2/tweets/20?tweet.fields=author_id,created_at,entities,source,public_metrics,lang,geo&expansions=author_id&user.fields=created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,url,username,verified,public_metrics";
// Create a new connection to the OAuth server, with a helper method
OAuthRequest client = OAuthRequest.ForProtectedResource("GET", CONSUMER_KEY, CONSUMER_TOKEN, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
client.RequestUrl = REQUEST_URL;
// add HTTP header authorization
string auth = client.GetAuthorizationHeader();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(client.RequestUrl);
request.Headers.Add("Authorization", auth);
Console.WriteLine("\nCalling " + REQUEST_URL + "\n");
// make the call and print the string value of the response JSON
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string strResponse = reader.ReadToEnd();
Console.WriteLine(strResponse); // we have a string (JSON)
}
}
// Source - https://stackoverflow.com/a/79676911
// Posted by Himanshu Kumar Sinha
// Retrieved 2025-12-01, License - CC BY-SA 4.0
public async Task ReplyToTweet(string replyText)
{
if (string.IsNullOrWhiteSpace(replyText))
{
return "Reply text cannot be empty.";
}
var ConsumerKey = "";
var ConsumerSecret = "";
var Token = "";
var TokenSecret = "";
var oAuth1 = OAuth1Authenticator.ForAccessToken(
consumerKey: ConsumerKey,
consumerSecret: ConsumerSecret,
token: Token,
tokenSecret: TokenSecret,
OAuthSignatureMethod.HmacSha256);
var options = new RestClientOptions("https://api.x.com")
{
Authenticator = oAuth1
};
var client = new RestClient(options);
var request = new RestRequest("2/tweets", Method.Post);
request.AddHeader("Content-Type", "application/json");
// Creating the body
var bodyObject = new
{
text = replyText
};
string body = JsonConvert.SerializeObject(bodyObject);
request.AddParameter("application/json", body, ParameterType.RequestBody);
var response = await client.ExecuteAsync(request);
if (response.IsSuccessful)
{
var responseData = JsonConvert.DeserializeObject(response.Content);
return $"tweet created successfully! Tweet ID: {responseData.data.id}";
}
else
{
return $"Failed to create reply tweet. Error: {response.ErrorMessage}\nResponse: {response.Content}";
}
}
KEINES davon funktioniert. Ich erhalte immer die Meldung „Nicht autorisiert“/„Authentifizierung nicht möglich“.
Ich bin am Ende meiner Kräfte und würde mich über jede Hilfe freuen, wie ich mit meinem Konto und allen Schlüsseln und Geheimnissen, auf die ich in meinem Entwicklerkonto Zugriff habe, ohne Browserauthentifizierung einen einfachen Tweet auf meinem Tweeter posten kann.
Ich versuche, eine unglaublich einfache C#-Konsolenanwendung zu schreiben, die in meinem Namen nur einen einzigen Tweet auf meinem Twitter postet. Ich habe ein Entwicklerkonto und lese die Referenz, einschließlich der Teile, die keinen Sinn ergeben und derjenigen, die sich widersprechen. Ich habe ältere Fragen und Twitter-Foren durchgesehen und egal, was ich versuche, ich kann mich nie authentifizieren. Mein Ziel ist es, NICHT zu einem Webbrowser umgeleitet zu werden, um mich manuell anzumelden Konto, aber verwenden Sie zur Authentifizierung die Schlüssel und Geheimnisse des Entwicklerkontos. Ich habe alles Folgende ausprobiert: [code]static async Task Main() { var consumerKey = ""; var consumerSecret = ""; var accessToken = ""; var accessTokenSecret = "";
var tweetText = "test";
var oauthNonce = Convert.ToBase64String(Encoding.ASCII.GetBytes(DateTime.Now.Ticks.ToString())); var oauthTimestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString();
var url = "https://api.twitter.com/1.1/statuses/update.json";
public static async Task ReplyToTweet(string replyText) { if (string.IsNullOrWhiteSpace(replyText)) { return "Reply text cannot be empty."; }
var ConsumerKey = CONSUMER_KEY; // also tried with API_KEY var ConsumerSecret = CONSUMER_SECRET; // also tried with API_SECRET var Token = ACCESS_TOKEN; var TokenSecret = ACCESS_TOKEN_SECRET;
var options = new RestClientOptions("https://api.x.com") { Authenticator = oAuth1 };
var client = new RestClient(options); var request = new RestRequest("2/tweets", Method.Post); request.AddHeader("Content-Type", "application/json");
// Creating the body var bodyObject = new { text = replyText };
string body = JsonSerializer.Serialize(bodyObject); request.AddParameter("application/json", body, ParameterType.RequestBody);
var response = await client.ExecuteAsync(request);
if (response.IsSuccessful) { var responseData = JsonSerializer.Deserialize(response.Content); return $"tweet created successfully! Tweet ID: {responseData.data.id}"; } else { return $"Failed to create reply tweet. Error: {response.ErrorMessage}\nResponse: {response.Content}"; } } [/code] . [code]class Program { static void Main(string[] args) {
Console.WriteLine("Enter the Tweet ID: "); string tweetID = Console.ReadLine(); // no error checking here!
// convenient to load keys and tokens from a config file for testing // edit .env.sample (and rename to .env) to add your keys and tokens (no quotation marks) DotNetEnv.Env.Load();
// this is the endpoint we will be calling StringBuilder apiPath = new StringBuilder("https://api.twitter.com"); apiPath.Append("/2/tweets"); apiPath.AppendFormat("?ids={0}", tweetID); apiPath.Append("&tweet.fields=attachments,author_id,context_annotations,created_at,entities,geo,id,in_reply_to_user_id,lang,possibly_sensitive,public_metrics,referenced_tweets,source,text,withheld"); apiPath.Append("&media.fields=duration_ms,height,media_key,preview_image_url,type,url,width"); apiPath.Append("&expansions=attachments.poll_ids,attachments.media_keys,author_id,geo.place_id,in_reply_to_user_id,referenced_tweets.id"); apiPath.Append("&poll.fields=duration_minutes,end_datetime,id,options,voting_status"); apiPath.Append("&place.fields=contained_within,country,country_code,full_name,geo,id,name,place_type"); apiPath.Append("&user.fields=created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified,withheld"); string REQUEST_URL = apiPath.ToString();
// if you would like to compare to v1.1 then this alternative REQUEST_URL does that for Tweet ID 20 // string REQUEST_URL = "https://api.twitter.com/labs/2/tweets/20?tweet.fields=author_id,created_at,entities,source,public_metrics,lang,geo&expansions=author_id&user.fields=created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,url,username,verified,public_metrics";
// Create a new connection to the OAuth server, with a helper method OAuthRequest client = OAuthRequest.ForProtectedResource("GET", CONSUMER_KEY, CONSUMER_TOKEN, ACCESS_TOKEN, ACCESS_TOKEN_SECRET); client.RequestUrl = REQUEST_URL;
// make the call and print the string value of the response JSON HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string strResponse = reader.ReadToEnd();
Console.WriteLine(strResponse); // we have a string (JSON) } } [/code] . [code]// Source - https://stackoverflow.com/a/79676911 // Posted by Himanshu Kumar Sinha // Retrieved 2025-12-01, License - CC BY-SA 4.0
public async Task ReplyToTweet(string replyText) { if (string.IsNullOrWhiteSpace(replyText)) { return "Reply text cannot be empty."; }
var ConsumerKey = ""; var ConsumerSecret = ""; var Token = ""; var TokenSecret = "";
var options = new RestClientOptions("https://api.x.com") { Authenticator = oAuth1 };
var client = new RestClient(options); var request = new RestRequest("2/tweets", Method.Post); request.AddHeader("Content-Type", "application/json");
// Creating the body var bodyObject = new { text = replyText };
string body = JsonConvert.SerializeObject(bodyObject); request.AddParameter("application/json", body, ParameterType.RequestBody);
var response = await client.ExecuteAsync(request);
if (response.IsSuccessful) { var responseData = JsonConvert.DeserializeObject(response.Content); return $"tweet created successfully! Tweet ID: {responseData.data.id}"; } else { return $"Failed to create reply tweet. Error: {response.ErrorMessage}\nResponse: {response.Content}"; } } [/code] KEINES davon funktioniert. Ich erhalte immer die Meldung „Nicht autorisiert“/„Authentifizierung nicht möglich“. Ich bin am Ende meiner Kräfte und würde mich über jede Hilfe freuen, wie ich mit meinem Konto und allen Schlüsseln und Geheimnissen, auf die ich in meinem Entwicklerkonto Zugriff habe, ohne Browserauthentifizierung einen einfachen Tweet auf meinem Tweeter posten kann.
Geben Sie hier die Bildbeschreibung ein
Ich versuche, Selenium in Python zu verwenden, um ein bestimmtes HTML-Element abzurufen, genauer gesagt seinen Wert.
``
Der Code gibt mir den Fehler 404 „Notfound“ aus, obwohl ich die Dokumentation befolge, wenn ich zu diesem Endpunkt „account/verify_credentials“ gehe.
Aber er funktioniert einwandfrei, wenn ich zum...
Ich verwende Selenium, um mich bei Twitter anzumelden (und zu posten), um API-Ratenbeschränkungen zu umgehen. Vor kurzem (ein paar Tage) erhalte ich direkt nach der Eingabe des Benutzernamens die...
Ich lade über Ajax etwas HTML in eine Seite, das ein neues tabulatorfähiges Element enthält. Das einzige Problem besteht darin, dass die Registerkarten nicht funktionieren, da Bootstrap bereits...