Advertisement
Icegoten

How to use API

Nov 6th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. One thing I wish someone had written down somewhere explicitly or just told me when I started to research API's such as Google's, is that most of these types of API's are not (always) a library or software that you install: They are web services.
  2. What this means is that in order to use the API, you yourself (or a library that is available, but this may not always be the case) use an HTTP library to actually access the API. The API consists of 'endpoints' which are simply URI's (note that there is very little difference between a URL and a URI in this case, for the time being it will also do to think of them as URL's).
  3. Then, you simply visit/call/load/request (whatever you want to call it) the URI's that you need using HTTP methods like GET and POST. Most API's require you to register your application if you intend to make use of the API, which will get you a private key that you send along with every request to the API. More often than not, if you don't register and thus don't have a key, the API will not allow you to get any data. An example would be this URI of the Facebook API: https://graph.facebook.com/search?q=conference&type=event (Yes, this is actually how easy it is to access most API's: It's just a URI that you visit in your code!)
  4. As you can see, it mentions an error because there is no access token being sent along with your request.
  5. This also brings us to how an API returns data: Most modern API's support the JSON format, some also support/use XML. The response you get when you visit that Facebook API URI is this:
  6. {
  7. "error": {
  8. "message": "An access token is required to request this resource.",
  9. "type": "OAuthException",
  10. "code": 104
  11. }
  12. }
  13. This is JSON format.
  14. Most modern languages provide JSON tools (either built-in or through a library) to convert a string (which is basically the textual response you get, see above) to an object in that language.
  15. For example, if you're using Python you could create a dictionary from that JSON string like so:
  16. import json
  17.  
  18. response_string = '{ "error": { "message": "An access token....'
  19. dict = json.loads(response_string)
  20. Now you could access the data like so:
  21. print dict["error"]["code"] # Will print '104'.
  22. print dict["error"]["type"] # Will print 'OAuthException'.
  23. So, basically what you want to do is find out:
  24. the endpoints of the API
  25. how the API's authorization is done (this can be through an HTTP authorization header that you have to send or maybe a query parameter, it depends on the API)
  26. how to perform HTTP requests in your code
  27. and then once you can successfully get the response data from such a request: Turn it into some format that is easy for you to access in your language.
  28. Lastly:
  29. Use the data you've received however you need to!
  30. Good luck!
  31. EDIT: To expand upon API libraries: Certain API's also come with libraries in one or more languages which are designed to help you and speed up the process of using the API. The API may for example have simple functions that your code can call to store your authorization key, and then more functions for each of the API's endpoints. That way the library allows you to simply call a function you need and the library takes care of accessing the proper endpoint, providing the authorization data in the right way, and possibly transforming the returned response data into something more convenient for you to use. Generally if there are API libraries available for the API you want to use in the language you want to use: Go for it! It will most likely make your life a ton less complicated!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement