This code is structured in three main parts to authenticate with Reddit’s API, collect data, and save it to a CSV file.
Once I receive the authorization code from Reddit, the next step is to exchange it for an access token. The script sends a POST request to Reddit’s token endpoint, including essential details like the client credentials (ID and secret), the authorization code, and the redirect URI (http://localhost).
If everything is set up correctly and the request is successful, Reddit returns an access token. This token is crucial because it allows the script to make further API requests without needing to re-authenticate. The token is stored and used to access Reddit data in subsequent steps. If the request fails, the script prints an error message and stops execution.
# Defineing credentials and the authorization code
client_id <- "VbGF******************"
client_secret <- "yiHZ*************************"
redirect_uri <- "http://localhost"
code <- "3VHUx*******************" # authorization code
# Make a POST request to exchange the code for an access token
token_response <- POST(
url = "https://www.reddit.com/api/v1/access_token",
authenticate(client_id, client_secret), # Client ID and Secret for basic auth
body = list(
grant_type = "authorization_code", # Grant type for code exchange
code = code,
redirect_uri = redirect_uri
),
encode = "form"
)
# Check the response
if (http_status(token_response)$category == "Success") {
token <- content(token_response, as = "parsed", type = "application/json")
print(token) # Access token and other details
} else {
print(content(token_response, as = "text"))
stop("Failed to exchange authorization code for access token.")
}
After collecting the data from Reddit, the script processes it and saves it to a CSV file named reddit_posts.csv. This step ensures that the data is stored locally for further analysis or sharing.
Once the file is successfully created, the script prints a confirmation message to let me know that the file has been saved without any issues. This provides a final checkpoint to verify that the data collection process was completed successfully.
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
# Save the dataset to a CSV file
write.csv(df, file = "reddit_posts.csv", row.names = FALSE)
# Confirm the file was saved
cat("File 'reddit_posts.csv' has been saved successfully!")