1

So over at the twitter api documentation, it says to generate a Bearer token you run this command

curl --user "$API_KEY:$API_SECRET_KEY" \
  --data 'grant_type=client_credentials' \
  'https://api.twitter.com/oauth2/token'

I run it and the result I get is the HTML for https://api.twitter.com/ouath2/token

Then I wrote some rust code,

extern crate reqwest;

use reqwest::{ Client };

// Twitter api endpoint - https://api.twitter.com/

#[tokio::main]
async fn main() -> Result<(), reqwest::Error>{
    let http_client =Client::new();

    // We need to get a bearer_token before we can access anything with the twitter api
    let bearer_token = http_client.post("https://api.twitter.com/oauth2/token")
        .basic_auth(API_KEY, Some(API_KEY_SECRET)
        .body("grant_type=client_credentials")
        .send().await?
        .text().await?;
    println!("{}", bearer_token);
    Ok(())
}

After executing the rust code above, this is what is printed,

{"errors":[{"code":170,"message":"Missing required parameter: grant_type","label":"forbidden_missing_parameter"}]}

However from the documentation the expected result is this:

{"token_type":"bearer","access_token":"AAAAAAAAAAAAAAAAAAAAAMLheAAAAAAA0%2BuSeid%2BULvsea4JtiGRiSDSJSI%3DEUifiRBkKG5E2XzMDjRfl76ZC9Ub0wnz4XsNiRVBChTYbJcE3F"}

Does anyone know why? Here's my cargo.toml file

[package]
name = "release"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1", features = ["full"] }

By the way, I applied for twitter developer access a few hours back, but I'm not sure on how to check if I do have access. I mean, I can make a project, and an app and access the dashboard.

HippoBaguette
  • 308
  • 4
  • 15
  • [It says](https://developer.twitter.com/en/docs/authentication/oauth-2-0/bearer-tokens) "You will need an approved developer account and must have created a developer App. If you would like to use the new Twitter API v2 endpoints, you will need to make sure that the developer App is within a Project." Is all of that true for your project? – isaactfa Jul 03 '21 at 12:05
  • The app is in a project and have a developer app. How do I check if I have an approved developer account? – HippoBaguette Jul 03 '21 at 12:41
  • [Apparently](https://developer.twitter.com/en/docs/developer-portal/faq#:~:text=There%20is%20no%20way%20to%20check%20the%20status%20of%20an%20application%20through%20the%20developer%20portal%20at%20this%20time%2C%20so%20please%20monitor%20your%20email%20for%20updates%20or%20requests%20for%20more%20information%3B%20those%20emails%20will%20continue%20to%20come%20from%20developer-accounts%40twitter.com.) there is no way to check. Did you get an email regarding the status of your application? – isaactfa Jul 03 '21 at 12:47
  • Hm alright I'm just going to assume its that, and wait for an email – HippoBaguette Jul 03 '21 at 23:33

2 Answers2

1

https://api.twitter.com/2/oauth2/token is the wrong URI, it should be https://api.twitter.com/oauth2/token

Andy Piper
  • 11,422
  • 2
  • 26
  • 49
  • Doesn’t that depend which API version one is trying to use? – eggyal Jul 03 '21 at 17:30
  • No, the OAuth endpoints are for both v1.1 and v2 at this time. – Andy Piper Jul 03 '21 at 21:19
  • Trying that the response is `{"errors":[{"code":170,"message":"Missing required parameter: grant_type","label":"forbidden_missing_parameter"}]}` but thanks for pointing out that mistake, I've edited the question now – HippoBaguette Jul 03 '21 at 23:38
0

Are there any brackets missing?

.basic_auth(API_KEY, Some(API_KEY_SECRET)
.basic_auth(API_KEY, Some(API_KEY_SECRET))