How to use HTTP Requests library with API keys

edited March 2018 in Library Questions

I'm quite new to using APIs and am trying to use the yelp API in processing to get images. I need some help using an API key in this library. Their website says to "put the API Key in the request header as "Authorization: Bearer <YOUR API KEY>" Here's my attempt...

import http.requests.*;

GetRequest get = new GetRequest("https://" + "api.yelp.com/v3/businesses/gary-danko-san-francisco");//Just a random example I found online. I know it's not an image.
get.addHeader("Authorization", "Bearer: <MY API KEY>");
//^ This is my attempt at using the API key
get.send();
println("Response Content: " + get.getContent());
println("Response Content-Length Header: " + get.getHeader("Content-Length"));

This gets printed in the console window

Response Content: {"error": {"code": "INVALID_AUTHORIZATION_METHOD", "description": "Invalid authorization method supplied."}}
Response Content-Length Header: 108

This is just a minimal reworking of the example code given in the HTTP requests library. As I said I'm very new to this and any pointers you could give would be much appreciated. Thanks in advance.

Answers

  • edited March 2018

    put the API Key in the request header as "Authorization: Bearer <YOUR API KEY>

    ...

    get.addHeader("Authorization", "Bearer: <MY API KEY>");

    spot the difference...

  • (there are no java examples in the yelp github but the php is pretty clear

    CURLOPT_HTTPHEADER => array(
                    "authorization: Bearer " . $GLOBALS['API_KEY'],
                    "cache-control: no-cache",
                ),
    

    and the ruby

    response = HTTP.auth("Bearer #{API_KEY}")
    
Sign In or Register to comment.