
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
github.com/Fazal-Rehaman07/Twitter_API
This Go-based project demonstrates how to interact with the Twitter API using OAuth1. In Assignment we have done the following:
Before running this project, you need to have a Twitter Developer Account and access to Twitter's API keys. Follow these steps:
After setting up your Twitter Developer Account:
Set the following environment variables with the Twitter credentials you've obtained:
For Linux/macOS, use:
export TWITTER_API_KEY="your-api-key"
export TWITTER_API_KEY_SECRET="your-api-key-secret"
export TWITTER_ACCESS_TOKEN="your-access-token"
export TWITTER_ACCESS_TOKEN_SECRET="your-access-token-secret"
For Windows (CMD), use:
set TWITTER_API_KEY=your-api-key
set TWITTER_API_KEY_SECRET=your-api-key-secret
set TWITTER_ACCESS_TOKEN=your-access-token
set TWITTER_ACCESS_TOKEN_SECRET=your-access-token-secret
go get github.com/dghubble/oauth1
go run main.go
localhost:8080
.To post a tweet, the /tweet
endpoint is used. This endpoint accepts a POST
request containing the tweet text in JSON format. The program will authenticate with Twitter and make a POST
request to the Twitter API to post the tweet.
POST http://localhost:8080/tweet
{
"text": "I am excited to share that I have developed a Twitter API in Go!"
}
{
"text": "I am excited to share that I have developed a Twitter API in Go!"
}
To delete a tweet, the /delete/{tweet_id}
endpoint is used. This endpoint accepts a DELETE
request, where {tweet_id}
is the unique ID of the tweet to be deleted. This can be found on the URL of the tweet E.g: https://x.com/FazalUrRehaman7/status/1844875797158412761. The program makes a DELETE
request to the Twitter API to remove the specified tweet.
DELETE http://localhost:8080/delete/1844875797158412761
Tweet deleted: 1844875797158412761
The program checks the status code of the response from Twitter's API to determine whether the request was successful:
201 Created
status is expected.200 OK
status is expected.If the response status is not as expected, the program logs an error and exits:
if resp.StatusCode != http.StatusCreated {
log.Fatalf("Error response from Twitter API: %s", resp.Status)
}
The program handles invalid input (e.g., malformed JSON) by returning an HTTP 400 Bad Request
response to the client:
if err := json.NewDecoder(r.Body).Decode(&tweet); err != nil {
http.Error(w, "Invalid request", http.StatusBadRequest)
return
}
If the DELETE
request for a tweet fails (e.g., the tweet doesn't exist), the program returns a 404 Not Found
error to the client:
if resp.StatusCode != http.StatusOK {
log.Fatalf("Error response from Twitter API: %s", resp.Status)
http.Error(w, "Tweet not found", http.StatusNotFound)
}
With this error handling approach, the program ensures graceful failure and communicates errors clearly to the client.
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.