\u003C/figure>\n\u003C!-- /wp:image -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>Consider a simple application, diagrammed above, which allows users to manage todos. Clients like a web browser or mobile app will access two different components: an OAuth platform to authenticate users and a Todo API to add, update, or delete todos. \u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>An OAuth grant is a specific flow that results in an access token. Per the specification, a token is an opaque string without any structure. However, OAuth servers can choose their token format, and many use JSON Web Tokens, which do have internal structure. Some parts of the grant, such as error messages or expected parameters, are well defined. Others, such as the actual authentication process, are left as implementation details. \u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>A grant authenticates the user or other entity, assembles appropriate permissions based on user roles, groups, and requested scopes, gathers the authorization data, and encapsulates those permissions from the authorization server in the form of an access token. Here’s an example of a token: \u003Ccode>mF_9.B5f-4.1JqM\u003C/code>. \u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>The token is often, but not always, sent to the client for later presentation to the resource server. In the diagram above, the mobile apps and browser on the left will be going through an OAuth grant in order to gain access to the Todo API.\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>In this context, which grant should you choose to send your users through? The core RFC, RFC 6749, defines a number of grants. It can be confusing to determine which is the best fit for your use case.\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>For most developers, there are really only a few questions to ask:\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:list -->\n\u003Cul>\u003C!-- wp:list-item -->\n\u003Cli>Is there a human being involved?\u003C/li>\n\u003C!-- /wp:list-item -->\n\n\u003C!-- wp:list-item -->\n\u003Cli>How long will the client need access to the protected resource?\u003C/li>\n\u003C!-- /wp:list-item -->\u003C/ul>\n\u003C!-- /wp:list -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>Let's tackle each question.\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:heading {\"level\":3} -->\n\u003Ch3 id=\"h-is-there-a-human-being-involved\">Is there a human being involved?\u003C/h3>\n\u003C!-- /wp:heading -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>Whenever a user is involved, the best grant to use is the Authorization Code grant. This grant will be discussed in more detail below. Using this offers architectural flexibility around the final location of the access token as well as a better security profile. In general, you should pair the Authorization Code grant with PKCE.\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>In the todo application outlined in the diagram above, a human being is making the initial request to display todos in their application, so the Authorization Code grant should be used.\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>But let's consider a different situation.\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>Suppose we extend the application with new functionality to remind our users of deadlines associated with their todos. \u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>In that case, we need a reminder service which would run every day, see which todos were due, and send a reminder email. This service would still need to authenticate because we wouldn’t want anyone to be able to access our todos. But there is no user kicking off the request—perhaps its only a cron job. \u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>That flow of permissions might look a little something like this:\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:image {\"id\":19930,\"sizeSlug\":\"full\",\"linkDestination\":\"none\"} -->\n\u003Cfigure class=\"wp-block-image size-full\">\u003Cimg src=\"https://stackoverflow.blog/wp-content/uploads/2022/04/Oauth-Diagram-2.png\" alt=\"\" class=\"wp-image-19930\"/>\u003C/figure>\n\u003C!-- /wp:image -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>When there is no human being starting the request, the correct grant to use is the \u003Ca href=\"https://datatracker.ietf.org/doc/html/rfc6749#page-40\">Client Credentials grant\u003C/a>. This grant should be used whenever you have service to service communication and want to leverage the client library support, centralized authentication, and security infrastructure of an authorization server.\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:heading {\"level\":3} -->\n\u003Ch3 id=\"h-how-long-will-the-client-need-access-to-the-protected-resource\">How long will the client need access to the protected resource?\u003C/h3>\n\u003C!-- /wp:heading -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>In general, access tokens are good for a short period of time (seconds to minutes). If the client can gain access to the resources it needs in that time, then all is well and good. However, sometimes the client needs access for the longer term. When the access token expires, the client can either ask the user to re-authenticate or it can use the Refresh grant. \u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>This grant allows a client to transparently re-request a token with the same permissions without forcing the user to re-authenticate.\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:heading {\"level\":3} -->\n\u003Ch3 id=\"h-what-about-the-other-grants\">What about the other grants?\u003C/h3>\n\u003C!-- /wp:heading -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>The other grants, such as the Device grant or the Resource Owner Password grant, should be used only when the use case calls for their specific functionality. \u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>In general, use the Authorization Code grant if there is a human being involved and the Client Credentials grant if you are performing server to server communication.\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:heading -->\n\u003Ch2 id=\"h-alternatives-to-oauth\">Alternatives to OAuth\u003C/h2>\n\u003C!-- /wp:heading -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>OAuth isn’t the only option to protect your API. The main alternative is API keys. They are a good solution in some situations and they are simple to understand. However, compared to OAuth, they do have some deficiencies.\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>API keys are relatively static. While you can and should rotate API keys, you have to build the infrastructure to do this yourself. API keys are not time-bound unless you also build this into your system.\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>API keys are “secrets” and should be managed as such. Just like the OAuth client secret, API keys are privileged data, which means you can’t, for example, store them safely in JavaScript. Therefore, they limit your architectural flexibility. \u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>There also is no encoded information in an API key, unlike tokens, which may have encoded information, especially if an access token is a JWT. This richer data format can include useful business-specific information such as a todo app subscription level. It also allows for authorization to be performed without requiring “phoning home” to the the OAuth server which created the token.\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:heading -->\n\u003Ch2 id=\"h-conclusion\">Conclusion\u003C/h2>\n\u003C!-- /wp:heading -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>In this article, you learned about why OAuth is a good choice to protect access to your APIs, more about its component standards, and options for using OAuth grants to protect resources. You also learned about alternatives to using OAuth such as API keys.\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>While OAuth can be complex, it handles a large number of use cases. You separate out the concern of authentication to a specialized component, while using a standardized temporary credential (the token) in the rest of your system. In addition, by using OAuth, you leverage standards and expertise from all over the world.\u003C/p>\n\u003C!-- /wp:paragraph -->\n\n\u003C!-- wp:paragraph -->\n\u003Cp>In the next article in this two part series, we'll look at how the Authorization Code grant works in step by step detail with code, as well as how you should validate a token.\u003C/p>\n\u003C!-- /wp:paragraph -->","html","2022-12-22T14:00:00.000Z",{"current":1030},"the-complete-guide-to-protecting-your-apis-with-oauth2",[1032,1039,1043,1048,1053,1058,1062],{"_createdAt":1033,"_id":1034,"_rev":1035,"_type":1036,"_updatedAt":1033,"slug":1037,"title":1038},"2023-05-23T16:43:21Z","wp-tagcat-authentication","9HpbCsT2tq0xwozQfkc4ih","blogTag",{"current":1038},"authentication",{"_createdAt":1033,"_id":1040,"_rev":1035,"_type":1036,"_updatedAt":1033,"slug":1041,"title":1042},"wp-tagcat-authorization",{"current":1042},"authorization",{"_createdAt":1033,"_id":1044,"_rev":1035,"_type":1036,"_updatedAt":1033,"slug":1045,"title":1047},"wp-tagcat-code-for-a-living",{"current":1046},"code-for-a-living","Code for a Living",{"_createdAt":1033,"_id":1049,"_rev":1035,"_type":1036,"_updatedAt":1033,"slug":1050,"title":1052},"wp-tagcat-oauth2",{"current":1051},"oauth2","OAuth2",{"_createdAt":1033,"_id":1054,"_rev":1035,"_type":1036,"_updatedAt":1033,"slug":1055,"title":1057},"wp-tagcat-rest-api",{"current":1056},"rest-api","rest api",{"_createdAt":1033,"_id":1059,"_rev":1035,"_type":1036,"_updatedAt":1033,"slug":1060,"title":1061},"wp-tagcat-security",{"current":1061},"security",{"_createdAt":1063,"_id":1064,"_rev":1065,"_type":1036,"_updatedAt":1066,"description":1067,"slug":1076,"title":1077},"2025-04-24T16:28:57Z","797b8797-6e65-4723-b53f-8bc005305384","vn3UzGZJyacwgllS8WZNgc","2025-04-24T16:29:32Z",[1068],{"_key":1069,"_type":65,"children":1070,"markDefs":1075,"style":74},"bb32f75814b4",[1071],{"_key":1072,"_type":69,"marks":1073,"text":1074},"dbcf27ef29b3",[],"Community-generated articles submitted for your reading pleasure. ",[],{"_type":10,"current":1077},"contributed","The complete guide to protecting your APIs with OAuth2 (part 1)",[1080,1086,1092,1094],{"_id":1081,"publishedAt":1082,"slug":1083,"sponsored":12,"title":1085},"1b0bdf8c-5558-4631-80ca-40cb8e54b571","2025-08-21T14:00:25.054Z",{"_type":10,"current":1084},"research-roadmap-update-august-2025","Research roadmap update, August 2025",{"_id":1087,"publishedAt":1088,"slug":1089,"sponsored":12,"title":1091},"5ff6f77f-c459-4080-b0fa-4091583af1ac","2025-08-20T14:00:00.000Z",{"_type":10,"current":1090},"documents-the-architect-s-programming-language","Documents: The architect’s programming language",{"_id":16,"publishedAt":17,"slug":1093,"sponsored":12,"title":20},{"_type":10,"current":19},{"_id":1095,"publishedAt":1096,"slug":1097,"sponsored":12,"title":1099},"f0807820-02d7-4fc5-845f-3d76514b81c0","2025-08-11T16:00:00.000Z",{"_type":10,"current":1098},"renewing-chat-on-stack-overflow","Renewing Chat on Stack Overflow ",{"count":1101,"lastTimestamp":1102},12,"2023-11-01T13:16:57Z",["Reactive",1104],{"$sarticleModal":1024},["Set"],["ShallowReactive",1107],{"sanity-6GIdX8sf_1Fk7i-tubaIEn_OVv_U9r4EwgaWP_vBXSk":-1,"sanity-comment-wp-post-19921-1755839773386":-1},"/2022/12/22/the-complete-guide-to-protecting-your-apis-with-oauth2/?cb=1"]