Authorization Code Workflow

Authorization Code Workflow

šŸ“˜

Make sure you've read through the overview and the prerequisites

Workflow

  1. Create a PKCE code verifier and code challenge.
  2. Redirect the user to Yelp's authorization page.
  3. After the user authorizes your application, Yelp redirects their browser back to your application with an authorization code.
  4. Redeem the authorization code for an access token.
  5. Use the access token to make requests on behalf of the user.

The workflow is the same for business users and consumers. Only the authorization page and the scopes are different. See Which Authorization Page to Use.

Step 1: Create a PKCE Code Verifier and Challenge

PKCE ties the authorization code to your application, so a stolen code is no use to anyone else. Yelp supports the S256 method only.

  1. Generate a random code_verifier of 43 to 128 characters, using only A-Z, a-z, 0-9, -, ., _ and ~.
  2. Set code_challenge to the base64url-encoded SHA-256 hash of the code_verifier, without padding.
  3. Keep the code_verifier until Step 3. Don't send it in the authorization request.
import base64
import hashlib
import secrets

code_verifier = secrets.token_urlsafe(64)
code_challenge = (
    base64.urlsafe_b64encode(hashlib.sha256(code_verifier.encode("ascii")).digest())
    .rstrip(b"=")
    .decode("ascii")
)

Step 2: Redirect the User to Yelp's Authorization Page

Your application must redirect the user's browser to Yelp's authorization URL. This is a GET request, and you must provide the parameters below. The user signs in if needed, reviews your application's name and the permissions (scopes) you requested, and chooses to continue or cancel.

šŸ“˜

Note

The authorization page contains an X-Frame-Options SAMEORIGIN header. This prevents the page from being embedded in a non-Yelp frame.

Which Authorization Page to Use

Use the page that matches the user's account type. A request can't mix business and consumer scopes, and a scope for the other account type is never granted.

UserAuthorization URLScopes
Business userhttps://biz.yelp.com/oauth2/authorizeBusiness scopes, such as leads
Consumerhttps://www.yelp.com/oauth2/authorizeConsumer scopes: projects and project_actions
GET https://biz.yelp.com/oauth2/authorize

Parameters

ParameterRequiredDescription
client_idRequiredID assigned by Yelp for the third-party system that will make user-authorized requests to Yelp.
redirect_uriRequiredWhere Yelp sends the user after they authorize your application, with the authorization code. It must use https and exactly match a redirect URI registered for your application. Yelp doesn't ignore a trailing slash, match a prefix, or fall back to a default.

Contact your Yelp relationship manager to register a redirect URI.
response_typeRequiredA string denoting the type of response. In the case of requesting an authorization code, this value will be code.
scopeRequiredA space delimited list of actions that the user is authorizing your application to perform.

Business scopes (use with biz.yelp.com):
leads - enables reading leads and responding to them.
r2r - enables respond to review permission.
r2r_get_businesses - enables the ability to retrieve a list of businesses that the business owner has "claimed" aka businesses that this user can respond to reviews for.
r2r_business_owner - enables the ability to retrieve information about the business owner like username and profile photo.

Consumer scopes (use with www.yelp.com):
projects - enables working with the consumer's projects.
project_actions - enables taking actions on the consumer's projects.

Yelp must enable your application for each scope, do not request scopes you don't have access to. Don't mix business and consumer scopes in one request.
stateRecommendedA unique string generated by your application for each request, to maintain state between the request and the callback. Yelp returns it unchanged on the redirect. Used to prevent CSRF
code_challengeRequiredThe PKCE code challenge from Step 1.
code_challenge_methodRequiredMust be S256.

Request Template

https://biz.yelp.com/oauth2/authorize?client_id=itk2LQ1r9e88jEjKiGae1w&redirect_uri=https%3A%2F%2Fwww.example.com%2Fredirect_endpoint&response_type=code&scope=leads&state=some_unique_string&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM&code_challenge_method=S256
https://www.yelp.com/oauth2/authorize?client_id=itk2LQ1r9e88jEjKiGae1w&redirect_uri=https%3A%2F%2Fwww.example.com%2Fredirect_endpoint&response_type=code&scope=projects%20project_actions&state=some_unique_string&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM&code_challenge_method=S256

The user will be shown a confirmation dialog naming your application and the permissions (scopes) you requested:

560

Example business user dialog on yelp.com

Response

Once the user has signed in and authorized your application, Yelp redirects the browser to your redirect_uri with these query parameters:

Parameter NameDescription
codeA unique code that your application uses to redeem an access token.
stateThe state you sent in the authorization request. Verify this is the same as in the request to Yelp to prevent CSRF
issThe issuer of the response: https://api.yelp.com/oauth2/biz if the user authorized on biz.yelp.com, or https://api.yelp.com/oauth2/consumer if they authorized on www.yelp.com.

Check iss before you use the code. It must be the issuer for the authorization page you used, as listed above. If it isn't, discard the response. This protects your application from a response that was meant for a different authorization server. See RFC 9207.

https://www.example.com/redirect_endpoint?code=54321&state=some_unique_string&iss=https%3A%2F%2Fapi.yelp.com%2Foauth2%2Fbiz

If the user chooses Cancel, they are sent to yelp.com and your application isn't notified. Your application doesn't receive a redirect.

Step 3: Redeem the Authorization Code for an Access Token

The authorization code expires in 5 minutes and can be used once, so redeem it immediately. Verify that state and iss match what you expect before you do.

Send the code to Get Access Token v4 (POST https://api.yelp.com/oauth2/token/v4), along with the code_verifier from Step 1 and the same redirect_uri you sent in Step 2. Send client_id and client_secret in the form body.

POST /oauth2/token/v4 HTTP/1.1
Host: api.yelp.com
Content-Type: application/x-www-form-urlencoded

client_id={client_id}&
client_secret={client_secret}&
grant_type=authorization_code&
code={code}&
redirect_uri={redirect_uri}&
code_verifier={code_verifier}

The response includes an access token that expires in 1 hour and a refresh token that expires in 90 days by default:

{
  "access_token": "<128_character_long_string>",
  "token_type": "Bearer",
  "expires_in": 3600,
  "expires_on": "2026-11-10T20:00:00+00:00",
  "refresh_token": "<128_character_long_string>",
  "refresh_token_expires_in": 7776000,
  "refresh_token_expires_on": "2027-02-08T19:00:00+00:00",
  "scope": "leads"
}

Check scope in the response. It lists the scopes the user actually granted, which can be fewer than you requested:

  • On biz.yelp.com, a scope that isn't enabled for your application is left out instead of causing an error. The authorization page only lists the scopes that remain. If none remain, it says your application isn't requesting any information, and the response has no scope field. A token with no scopes can't call any Yelp API, so check which scopes are enabled for your application.
  • On www.yelp.com, a scope that is unknown or isn't enabled for your application stops the request with an error page instead (see Errors on the Authorization Page).

When the access token expires, use the refresh token to get a new one (see Step 5).

Endpoint documentation:

🚧

TOKEN_V4_REQUIRED

If Get Access Token v2 or v3 returns 400 with the error code TOKEN_V4_REQUIRED, your application must use v4. See Get Access Token v2 and v3.

Step 4: Use the Access Token to Make Requests on Behalf of the User

Once you have the access token you can start making API calls to all oauth secured Yelp APIs which are included in the scopes you have requested. You must include the access token in the Authorization header of the HTTP call.

Request Template

curl -X GET https://partner-api.yelp.com/token/v1/businesses -H 'Authorization: Bearer <access_token>'

Example: Retrieve businesses associated with access token

You can retrieve a list of all open businesses associated with a specific business user's access token.

Endpoint documentation:

Consumer Access Tokens

A consumer's access token works with the consumer endpoints in the Home Services API, such as listing the consumer's projects and sending messages in their conversations. Each endpoint acts only on the consumer who authorized your application.

A business user's access token only works on business endpoints, and a consumer's access token only works on consumer endpoints. If you serve both, keep their tokens apart and call each endpoint with the right one. See Errors When Calling an Endpoint.

Step 5: Refresh the Access Token

Every refresh on v4 returns a new refresh token, and the one you sent stops working. Store the new refresh token every time. For the request, the retry window and the errors, see the Refresh Tokens guide:

Revoke Access Token

The authorization server responds with HTTP status code 200 if the token has been revoked successfully or if the client submitted an invalid token.

Endpoint documentation:

Token Lifetimes

v4v2 and v3
Authorization code5 minutes5 minutes
Access token1 hour7 days
Refresh token90 days by default365 days by default

Use the expiry fields in each token response (expires_on and refresh_token_expires_on) rather than hard-coding the lifetimes. If you have an agreement with Yelp for longer-lived refresh tokens, contact your Yelp relationship manager before you move to v4.

Error Scenarios

Errors on the Authorization Page

Yelp doesn't redirect to your redirect_uri when an authorization request is invalid, and it doesn't send an error parameter. The user sees an error page that says "Woops! Something doesn't look right. Please contact the client where this request came from." The page doesn't say what was wrong, and your application isn't notified.

If a user reports this page, check the following:

  • code_challenge is missing, or code_challenge_method isn't S256.
  • redirect_uri is missing, doesn't use https, or isn't an exact match for a registered redirect URI.
  • response_type isn't code.
  • On www.yelp.com: scope is missing, or has a value that is unknown, isn't enabled for your application, or is a business scope, or it mixes business and consumer scopes.
  • On biz.yelp.com: scope includes a consumer scope that is enabled for your application. A scope that isn't enabled doesn't cause an error here. It's left out of the authorization (see Step 3).
  • client_id is unknown, or the application is disabled.

🚧

Because there is no redirect, your application can't tell a canceled request from one that was never completed. Treat a request that doesn't come back as abandoned, and let the user start again.

Errors When Redeeming the Code

Get Access Token v4 errors use the OAuth 2.1 format: a string error code and an error_description.

{
  "error": "invalid_grant",
  "error_description": "Auth code not found or expired."
}
HTTP CodeError CodeDescription
400 - Bad Requestinvalid_requestA required parameter is missing or invalid. This includes a missing code_verifier.
400 - Bad Requestinvalid_grantThe authorization code is unknown, expired or already used, the code_verifier doesn't match the code_challenge, or the redirect_uri doesn't match the one you sent in Step 2.
400 - Bad Requestinvalid_clientThe client_id or client_secret is missing or wrong.
400 - Bad Requestinvalid_scopeThe request includes a scope your application isn't allowed to use.
401 - Unauthorizedinvalid_clientClient authentication failed after the request included an Authorization header. Send client_id and client_secret in the form body.
429 - Too Many Requeststoo_many_requestsYou have exceeded the rate limit. Try again later.
500 - Internal Server Errorserver_errorServer error.

If the code is rejected with invalid_grant, start again from Step 1 with a new code challenge. A code can't be retried.

Errors When Calling an Endpoint

These errors use the Yelp format. They mean the access token is valid but can't be used on this endpoint:

HTTP CodeError CodeDescription
401 - UnauthorizedUNAUTHORIZED_ACCESS_TOKENOn a business endpoint, the token doesn't have a scope the endpoint needs. This includes a consumer's access token, which never has business scopes.
403 - ForbiddenMISSING_SCOPEOn a consumer endpoint, the token doesn't have a scope the endpoint needs. The description names the missing scopes, and the WWW-Authenticate header lists every scope the endpoint needs. This includes a business user's access token, which never has consumer scopes.
403 - ForbiddenUNSUPPORTED_USER_TYPEThe token has the scopes the endpoint needs, but it was issued for the other kind of user.
{
  "error": {
    "code": "MISSING_SCOPE",
    "description": "The access token doesn't have the scopes this endpoint needs: project_actions."
  }
}

Refreshing the token doesn't add scopes. To get a missing scope, send the user through the authorization page again and request it.

Get Access Token v2 and v3

šŸ“˜

Use v4 for new integrations

Some existing applications can still use v2 and v3, and they keep working as described here. We recommend moving to v4. If either endpoint returns TOKEN_V4_REQUIRED, your application must use v4.

For these applications:

  • code_challenge and code_challenge_method are optional on the authorization request. If you send code_challenge, you must also send code_challenge_method=S256, and then send the code_verifier when you redeem the code. Yelp can require PKCE for your application.
  • redirect_uri is optional. If you leave it out, Yelp uses the default redirect URI registered for your application. A redirect_uri you send must be on your allowlist, and if you send one on the authorization request, send the same one when you redeem the code.
  • The redirect doesn't include iss.
  • Access tokens expire in 7 days and refresh tokens in 365 days. See Token Lifetimes.
  • Errors use the Yelp format, {"error": {"code": …, "description": …}}, instead of the OAuth 2.1 format.

Business scopes and biz.yelp.com work as described in this guide. Consumer scopes and www.yelp.com are available only with the v4 rules.


What’s Next

Did this page help you?