HomeBlogErrors / Fixeshow to set urn:ietf:wg:oauth:2.0:oob as redirect uri in google auth 2.0
Errors / FixesSeptember 5, 20263 min

how to set urn:ietf:wg:oauth:2.0:oob as redirect uri in google auth 2.0

How to Set Redirect URI as `urn:ietf:wg:oauth:2.0:oob` in Google Auth 2.0 ## Introduction When working with OAuth 2.0 for user authentication in Google, sometimes there are issues with redirection...

how to set urn:ietf:wg:oauth:2.0:oob as redirect uri in google auth 2.0
how to set urn:ietf:wg:oauth:2.0:oob as redirect uri in google auth 2.0 - image 2

How to Set Redirect URI as urn:ietf:wg:oauth:2.0:oob in Google Auth 2.0

Introduction

When working with OAuth 2.0 for user authentication in Google, sometimes there are issues with redirection after obtaining the authorization code. This guide will cover how to set the redirect URI as urn:ietf:wg:oauth:2.0:oob for use in WordPress and how to resolve any arising errors.

The Problem and Its Solution

During work with the Google Analytics API through OAuth 2.0 in WordPress, the author was unable to get the authorization code within the same window and was instead redirected to another page. This led to issues with obtaining the access token.

The Used Solution

To solve the problem, it was decided to use urn:ietf:wg:oauth:2.0:oob as the redirect URI. This is a special URI used for manually entering the authorization code. This method allows you to avoid redirecting the user to a third-party site.

Steps for Configuration

  1. Create a New Application Client Go to the Google Console and create a new client application for desktop or installed application. This will allow you to specify urn:ietf:wg:oauth:2.0:oob as the redirect URI.

  2. Configure Redirect URI In the Google Console client settings, specify urn:ietf:wg:wg:oauth:2.0:oob as the redirect URI.

  3. Modify the Authorization Code Modify your authorization code to use this redirect URI.

if (isset($_POST['save_code']) && isset($_POST['access_code'])) {
    $authCode = $_POST['access_code'];
    $client = new Google_Client();
    $client->setClientId('***');
    $client->setClientSecret('***');
    $client->setDeveloperKey('***');
    $client->setScopes([
        'https://www.googleapis.com/auth/analytics',
        'openid',
        'https://www.googleapis.com/auth/analytics.readonly'
    ]);
    $client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob'); // Setting the redirect URI
    $client->setAccessType('offline');
    $client->setIncludeGrantedScopes(true);
    $client->authenticate($authCode);
    $access_token = $client->getAccessToken();
    var_dump($access_token);
}
  1. Create an Authentication URL Create an authentication URL using the necessary parameters.
$url = http_build_query(array(
    'next' => 'http://myproject.local.com/wp-admin/admin.php?page=analytify-settings',
    'scope' => 'https://www.googleapis.com/auth/analytics',
    'response_type' => 'code',
    'redirect_uri' => 'urn:ietf:wg:oauth:2.0:oob', // Specifying the redirect URI
    'client_id' => '***'
));
  1. Open the Authentication Window Open the authentication window using JavaScript.
<a target="_blank" href="javascript:void(0);" onclick="window.open('https://accounts.google.com/o/oauth2/auth?<?php echo $url ?>', 'activate', 'width=700,height=500,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=0,top=0');">Click here...</a>
  1. Obtain the Authorization Code After authorization, the user should manually enter the authorization code into your application.

Error Handling

After successful authorization, you should receive the authorization code and use it to obtain the access token. However, if you encounter the error ACCESS_TOKEN_SCOPE_INSUFFICIENT, it means that the provided permissions are insufficient for the operation.

Solving the ACCESS_TOKEN_SCOPE_INSUFFICIENT Error

  1. Check Permissions Ensure that all necessary permissions are listed in the settings of your application client.

  2. Update Permissions If permissions are insufficient, update them in the Google Console and repeat the authorization process.

  3. Check the API Ensure that the API to which you are trying to gain access indeed supports the specified permissions.

Conclusion

Using urn:ietf:wg:oauth:2.0:oob as the redirect URI allows you to avoid redirects and provides a more convenient authorization process. Following the above steps, you can successfully configure authorization in Google OAuth 2.0 in your WordPress application.

Practical Tips

  • Ensure that your Google application client has the appropriate permissions.
  • Verify that the redirect URI is correctly configured in the Google Console.
  • Use urn:ietf:wg:oauth:2.0:oob only for manually entering the authorization code.

FAQ

Q: How do I set up the redirect URI in the Google Console?

A: Go to the "One-Time URI Redirects" section in the Google Console and add urn:ietf:wg:oauth:2.0:oob.

Q: What should I do if I encounter the ACCESS_TOKEN_SCOPE_INSUFFICIENT error?

A: Check the permissions in the settings of your application client and ensure they include all necessary APIs.