Search code examples
ruby-on-railsvimeohttpartyvimeo-api

Can not send header to vimeo using HTTParty


I want to get unauthenticated access token from vimeo api in my rails app. However the post request made using HTTParty gem returns following response from API

{"error"=>"You must provide a valid authenticated access token."}

The code to send request is

header = "basic " + Base64.encode64("****07974be" + ":" + "****ygYBI7I")

token = HTTParty.post("https://api.vimeo.com/oauth/authorize/client",
          :body => {:grant_type => 'client_credentials'},
          :header => {'Authorization' => header}
        )

json=JSON.parse(token)

I have checked that credentials are correct and also tried replacing :header with :headers, and various combinations of using string instead of symbol in the header hash. But none of them works.

However, the call to same URL, using same credentials is successful through Postman.

Edit As mentioned in a answer, we need to use headers (plural) while making the call. However, I had already tried that but problem persists. Using basic_auth, instead of sending headers do seems to work, however I can not figure out why sending headers through HTTParty is not working but similar call is working through Postman.


Solution

  • The :headers option is definitely plural, but since you are using basic auth, you can also use HTTParty's basic auth option. So your request would become:

    username = "YOUR-USER-HERE"
    password = "YOUR-PASSWORD-HERE"
    
    token = HTTParty.post("https://api.vimeo.com/oauth/authorize/client",
          body: {:grant_type => 'client_credentials'},
          basic_auth: { username: username, password: password }
        )
    

    Using your creds (did you mean to post real creds?) I got

    {"access_token"=>"REDACTED", "token_type"=>"bearer", "scope"=>"public", "app"=>{"name"=>"Fable", "uri"=>"/apps/REDACTED"}}