Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruby On Rails: Update link text #28312

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions ruby_on_rails/apis/working_with_external_apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

If you go to the documentation for an API, it can sometimes be a bit overwhelming because much of the material sort of assumes that you know what you're doing, but some are definitely better than others. There are also elements that are common across almost all of them. The more you work with APIs (and get the hang of the authentication flow), the easier they get. You'll be making mashups in no time.

This lesson will cover some general steps that are common across APIs and will do high level overviews of some of the methods for authenticating with APIs like Omniauth. Try to gain as much conceptual understanding as you can here and use the documentation each gem or API provides to help with the implementation. If you find great free learning resources that explain APIs, please let us know [(make a pull request and add an additional resource at the bottom of this page)](https://github.com/TheOdinProject/curriculum)!
This lesson will cover some general steps that are common across APIs and will do high level overviews of some of the methods for authenticating with APIs like Omniauth. Try to gain as much conceptual understanding as you can here and use the documentation each gem or API provides to help with the implementation. If you find great free learning resources that explain APIs, you can [suggest them via a GitHub issue in our curriculum repo](https://github.com/TheOdinProject/curriculum/issues/new/choose)!
MaoShizhong marked this conversation as resolved.
Show resolved Hide resolved

### Lesson overview

Expand All @@ -32,9 +32,9 @@
Most APIs require a different type of "security clearance" for different requests:

1. You can usually make innocuous requests like asking for posts from X's (formerly known as Twitter) API with straightforward and unauthenticated GET requests. You can make these from any command line or a browser tool like [Chrome's Rest Client](http://getpostman.com). These types of API requests are usually severely rate limited to incentivize you to sign up.
2. The next layer is making requests that include your API key. These are still usually fairly innocuous things (like getting public data) and limited by the API's pricing tiers.

Check failure on line 35 in ruby_on_rails/apis/working_with_external_apis.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Ordered lists must always use 1. as a prefix (lazy numbering)

ruby_on_rails/apis/working_with_external_apis.md:35 TOP010/lazy-numbering-for-ordered-lists Ordered lists must always use 1. as a prefix (lazy numbering) [ Expected: "1" Actual: "2" ] https://github.com/TheOdinProject/curriculum/blob/main/markdownlint/docs/TOP010.md

Check failure on line 35 in ruby_on_rails/apis/working_with_external_apis.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Ordered lists must always use 1. as a prefix (lazy numbering)

ruby_on_rails/apis/working_with_external_apis.md:35 TOP010/lazy-numbering-for-ordered-lists Ordered lists must always use 1. as a prefix (lazy numbering) [ Expected: "1" Actual: "2" ] https://github.com/TheOdinProject/curriculum/blob/main/markdownlint/docs/TOP010.md
3. More sensitive requests like asking for specific user data or submitting/modifying/deleting data will likely require you to use an authentication process involving your secret token. We'll cover the basics of this in the project. Rates for these requests are subject to the API pricing tiers.

Check failure on line 36 in ruby_on_rails/apis/working_with_external_apis.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Ordered lists must always use 1. as a prefix (lazy numbering)

ruby_on_rails/apis/working_with_external_apis.md:36 TOP010/lazy-numbering-for-ordered-lists Ordered lists must always use 1. as a prefix (lazy numbering) [ Expected: "1" Actual: "3" ] https://github.com/TheOdinProject/curriculum/blob/main/markdownlint/docs/TOP010.md

Check failure on line 36 in ruby_on_rails/apis/working_with_external_apis.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Ordered lists must always use 1. as a prefix (lazy numbering)

ruby_on_rails/apis/working_with_external_apis.md:36 TOP010/lazy-numbering-for-ordered-lists Ordered lists must always use 1. as a prefix (lazy numbering) [ Expected: "1" Actual: "3" ] https://github.com/TheOdinProject/curriculum/blob/main/markdownlint/docs/TOP010.md
4. Oftentimes, you actually want to make requests on behalf of a user. For instance, showing a user a dashboard of all their posts and Facebook posts together would require asking X for that user's posts and Facebook for that user's posts. This can involve a LOT of requests over a large user base, but luckily you're actually able to make them on the user's behalf by asking for the user's permission. We'll cover this as well later, but basically you send the user to the API provider's site to sign in, then the API provider will give you a user-specific token to use when making requests on their behalf in the future. Rates for this are usually more advantageous because they are covered in a per-user bucket. We typically use the OAuth protocol for this, as described below.

Check failure on line 37 in ruby_on_rails/apis/working_with_external_apis.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Ordered lists must always use 1. as a prefix (lazy numbering)

ruby_on_rails/apis/working_with_external_apis.md:37 TOP010/lazy-numbering-for-ordered-lists Ordered lists must always use 1. as a prefix (lazy numbering) [ Expected: "1" Actual: "4" ] https://github.com/TheOdinProject/curriculum/blob/main/markdownlint/docs/TOP010.md

Check failure on line 37 in ruby_on_rails/apis/working_with_external_apis.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Ordered lists must always use 1. as a prefix (lazy numbering)

ruby_on_rails/apis/working_with_external_apis.md:37 TOP010/lazy-numbering-for-ordered-lists Ordered lists must always use 1. as a prefix (lazy numbering) [ Expected: "1" Actual: "4" ] https://github.com/TheOdinProject/curriculum/blob/main/markdownlint/docs/TOP010.md

### Versions

Expand Down Expand Up @@ -77,32 +77,32 @@
Basically (still using Facebook as an example):

1. User tries to access a page on your app and you ask the user to login
2. User chooses the "Login With Facebook" option

Check failure on line 80 in ruby_on_rails/apis/working_with_external_apis.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Ordered lists must always use 1. as a prefix (lazy numbering)

ruby_on_rails/apis/working_with_external_apis.md:80 TOP010/lazy-numbering-for-ordered-lists Ordered lists must always use 1. as a prefix (lazy numbering) [ Expected: "1" Actual: "2" ] https://github.com/TheOdinProject/curriculum/blob/main/markdownlint/docs/TOP010.md

Check failure on line 80 in ruby_on_rails/apis/working_with_external_apis.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Ordered lists must always use 1. as a prefix (lazy numbering)

ruby_on_rails/apis/working_with_external_apis.md:80 TOP010/lazy-numbering-for-ordered-lists Ordered lists must always use 1. as a prefix (lazy numbering) [ Expected: "1" Actual: "2" ] https://github.com/TheOdinProject/curriculum/blob/main/markdownlint/docs/TOP010.md
3. User is redirected to a Facebook page asking them to review the permissions you are asking for and telling them to sign in. The URI will contain parameters that tell Facebook who your application is and possibly which URI they should submit their response to (or maybe you specified this as a part of your API registration process with them).

Check failure on line 81 in ruby_on_rails/apis/working_with_external_apis.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Ordered lists must always use 1. as a prefix (lazy numbering)

ruby_on_rails/apis/working_with_external_apis.md:81 TOP010/lazy-numbering-for-ordered-lists Ordered lists must always use 1. as a prefix (lazy numbering) [ Expected: "1" Actual: "3" ] https://github.com/TheOdinProject/curriculum/blob/main/markdownlint/docs/TOP010.md

Check failure on line 81 in ruby_on_rails/apis/working_with_external_apis.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Ordered lists must always use 1. as a prefix (lazy numbering)

ruby_on_rails/apis/working_with_external_apis.md:81 TOP010/lazy-numbering-for-ordered-lists Ordered lists must always use 1. as a prefix (lazy numbering) [ Expected: "1" Actual: "3" ] https://github.com/TheOdinProject/curriculum/blob/main/markdownlint/docs/TOP010.md
4. User decides you seem like a fun application so they'll allow you to see their email address and post to their timeline. User signs in to their Facebook account. Facebook creates an authorization code and sends it back to your application's callback URI.

Check failure on line 82 in ruby_on_rails/apis/working_with_external_apis.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Ordered lists must always use 1. as a prefix (lazy numbering)

ruby_on_rails/apis/working_with_external_apis.md:82 TOP010/lazy-numbering-for-ordered-lists Ordered lists must always use 1. as a prefix (lazy numbering) [ Expected: "1" Actual: "4" ] https://github.com/TheOdinProject/curriculum/blob/main/markdownlint/docs/TOP010.md

Check failure on line 82 in ruby_on_rails/apis/working_with_external_apis.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Ordered lists must always use 1. as a prefix (lazy numbering)

ruby_on_rails/apis/working_with_external_apis.md:82 TOP010/lazy-numbering-for-ordered-lists Ordered lists must always use 1. as a prefix (lazy numbering) [ Expected: "1" Actual: "4" ] https://github.com/TheOdinProject/curriculum/blob/main/markdownlint/docs/TOP010.md
5. The user waits while your application takes that authorization code and uses it to ask Facebook for the real good stuff. Facebook makes sure your application is the same one the user authorized, then POSTs back to you a unique authentication token for the user (which likely expires in 90 days) and any data you asked for up front (like email address).

Check failure on line 83 in ruby_on_rails/apis/working_with_external_apis.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Ordered lists must always use 1. as a prefix (lazy numbering)

ruby_on_rails/apis/working_with_external_apis.md:83 TOP010/lazy-numbering-for-ordered-lists Ordered lists must always use 1. as a prefix (lazy numbering) [ Expected: "1" Actual: "5" ] https://github.com/TheOdinProject/curriculum/blob/main/markdownlint/docs/TOP010.md

Check failure on line 83 in ruby_on_rails/apis/working_with_external_apis.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Ordered lists must always use 1. as a prefix (lazy numbering)

ruby_on_rails/apis/working_with_external_apis.md:83 TOP010/lazy-numbering-for-ordered-lists Ordered lists must always use 1. as a prefix (lazy numbering) [ Expected: "1" Actual: "5" ] https://github.com/TheOdinProject/curriculum/blob/main/markdownlint/docs/TOP010.md
5. You store the user's unique token in your database and use it, along with your application key(s), to make any subsequent requests on the user's behalf.

Check failure on line 84 in ruby_on_rails/apis/working_with_external_apis.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Ordered lists must always use 1. as a prefix (lazy numbering)

ruby_on_rails/apis/working_with_external_apis.md:84 TOP010/lazy-numbering-for-ordered-lists Ordered lists must always use 1. as a prefix (lazy numbering) [ Expected: "1" Actual: "5" ] https://github.com/TheOdinProject/curriculum/blob/main/markdownlint/docs/TOP010.md

Check failure on line 84 in ruby_on_rails/apis/working_with_external_apis.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Ordered lists must always use 1. as a prefix (lazy numbering)

ruby_on_rails/apis/working_with_external_apis.md:84 TOP010/lazy-numbering-for-ordered-lists Ordered lists must always use 1. as a prefix (lazy numbering) [ Expected: "1" Actual: "5" ] https://github.com/TheOdinProject/curriculum/blob/main/markdownlint/docs/TOP010.md

See [this brief overview of OAuth 2.0](http://tutorials.jenkov.com/oauth2/overview.html) for an overview. Then check out [this more substantive explanation from tutsplus.com](https://code.tutsplus.com/articles/oauth-20-the-good-the-bad-the-ugly--net-33216).
See this brief [overview of OAuth 2.0](http://tutorials.jenkov.com/oauth2/overview.html). Then check out this [more substantive explanation of OAuth 2.0 from tutsplus.com](https://code.tutsplus.com/articles/oauth-20-the-good-the-bad-the-ugly--net-33216).

#### Implementing OAuth 2.0 in Rails -- Use OmniAuth!

This sounds horribly complicated! Someone must have made a gem for it...

Luckily someone has. Many someones, actually. There is a generic OAuth gem called `omniauth` (docs available [on GitHub](https://github.com/intridea/omniauth)) and then a separate gem which provides a specific authentication strategy for every major API (see the list [HERE](https://github.com/intridea/omniauth/wiki/List-of-Strategies)). Once you've gone through things with one of them, you'll become comfortable with all of them. It's also worth noting that if you install and use [Devise](https://github.com/heartcombo/devise) (to handle your user model, for example), it comes with support for Omniauth built in! Refer to the [documentation](https://github.com/heartcombo/devise/wiki/OmniAuth%3A-Overview) for details.
Luckily someone has. Many someones, actually. There is a generic OAuth gem called `omniauth` ([Omniauth documentation](https://github.com/intridea/omniauth)) and then a separate gem which provides a specific authentication strategy for every major API ([list of Omniauth strategies](https://github.com/intridea/omniauth/wiki/List-of-Strategies)). Once you've gone through things with one of them, you'll become comfortable with all of them. It's also worth noting that if you install and use [Devise](https://github.com/heartcombo/devise) (to handle your user model, for example), it comes with support for Omniauth built in! They have documentation on [how to integrate Devise with Omniauth](https://github.com/heartcombo/devise/wiki/OmniAuth%3A-Overview).

Using Omniauth is much easier to learn by doing than reading a bunch of bullet points. You'll have the opportunity to implement it in your final project where you can ask questions if needed.

### SDKs

In addition to or instead of API access, many companies provide SDKs (software development kits). Usually these are Javascript libraries that contain all the code necessary to access their API. This can be useful because you're then able to access the API with Javascript methods instead of doing backflips on your own backend. It comes with the downside, however, of expanding your code base and forcing you to use their conventions for everything.

Check failure on line 98 in ruby_on_rails/apis/working_with_external_apis.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Proper names should have the correct capitalization

ruby_on_rails/apis/working_with_external_apis.md:98:118 MD044/proper-names Proper names should have the correct capitalization [Expected: JavaScript; Actual: Javascript] https://github.com/DavidAnson/markdownlint/blob/v0.32.1/doc/md044.md

Check failure on line 98 in ruby_on_rails/apis/working_with_external_apis.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Proper names should have the correct capitalization

ruby_on_rails/apis/working_with_external_apis.md:98:264 MD044/proper-names Proper names should have the correct capitalization [Expected: JavaScript; Actual: Javascript] https://github.com/DavidAnson/markdownlint/blob/v0.32.1/doc/md044.md

We won't cover SDKs explicitly in this course but they should be well within reach to pick up by reading the documentation.

### Assignment

<div class="lesson-content__panel" markdown="1">
1. Watch [this Railscast on using Omniauth to allow X (formerly known as Twitter) Signin](http://railscasts.com/episodes/241-simple-omniauth-revised).
1. Watch this Railscast on [using Omniauth to allow X (formerly known as Twitter) Signin](http://railscasts.com/episodes/241-simple-omniauth-revised).
2. Read through the [Omniauth documentation](https://github.com/intridea/omniauth)
3. Pick an API that you really might like to interface with or a web product you use almost every day (e.g. Google, Facebook, Instagram...). Google for its docs, e.g. with "instagram api documentation", and have a look at them. Some docs are better than others, but they will be your source for understanding which methods you can call, what they will return, how to register your application to get an API key, and more useful tidbits.
</div>
Expand All @@ -127,7 +127,7 @@

This section contains helpful links to related content. It isn't required, so consider it supplemental.

- See [This SO Post on interfacing with third-party APIs](http://stackoverflow.com/questions/6228870/interfacing-with-a-third-party-api-in-rails-opeing-urls-and-parsing-xml-json) for tips.
- See this Stack Overflow post on [interfacing with third-party APIs](http://stackoverflow.com/questions/6228870/interfacing-with-a-third-party-api-in-rails-opeing-urls-and-parsing-xml-json) for tips.
- [RailsConf 2016 - From Zero to API Hero: Consuming APIs like a Pro by Cecy Correa](https://www.youtube.com/watch?v=Af5HDgvGuXk)
- Take a look at [this Medium article](https://revs.runtime-revolution.com/integrating-a-third-party-api-with-rails-5-134f960ddbba) over integrating a third party API with Rails 5.
- See this other [Medium Article](https://medium.com/food4fluctuations/using-an-api-in-rails-for-noobs-5e02edb0e56b) on creating a basic rails app using the OMDB API, an open source movie database.
- Take a look at this Medium article over [integrating a third party API with Rails 5](https://revs.runtime-revolution.com/integrating-a-third-party-api-with-rails-5-134f960ddbba).
- See this other Medium Article on [creating a basic rails app using the OMDB API](https://medium.com/food4fluctuations/using-an-api-in-rails-for-noobs-5e02edb0e56b), an open source movie database.
MaoShizhong marked this conversation as resolved.
Show resolved Hide resolved
Loading