When implementing the WhatsApp embedded signup flow, I can redirect to the signup flow an retrieve an access code. When I want to exchange the access code for an access code, I get the following error message:
OAuthException: Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request
My code:
const fbAsyncInit = function () {
// JavaScript SDK configuration and setup
FB.init({
appId: '<APP_ID>', // Meta App ID
cookie: true, // enable cookies
xfbml: true, // parse social plugins on this page
version: 'v18.0' //Graph API version
});
};
fbAsyncInit();
function launchWhatsAppSignup() {
// Launch Facebook login
FB.login(function (response) {
if (response.authResponse) {
const code = response.authResponse.code;
//Use this token to call the debug_token API and get the shared WABA's ID
resource.storeCredentials({token: code})
.then((response) => {
router.push({name: 'integration.whatsapp.select-phone-number'})
})
.catch(setError);
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, {
config_id: '<CONFIG_ID>', // configuration ID obtained in the previous step goes here
response_type: 'code', // must be set to 'code' for System User access token
override_default_response_type: true,
extras: {
setup: {
// Prefilled data can go here
}
}
});
}
When I want to exchange the code for an access token using the following code:
$response = Http::get('https://graph.facebook.com/v18.0/oauth/access_token', [
'client_id' => env('FACEBOOK_CLIENT_ID'),
'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
'code' => $this->code,
]);
I get the following error:
Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request
I used my localhost to test this out, I also did this on a live environment with a URL I specified in the Facebook login settings, but without any luck.
Any Idea what I’m doing wrong?
2
Answers
The problem is that that despite FB’s docs being unclear about it,
redirect_uri
is a required parameter on your call tohttps://graph.facebook.com/v18.0/oauth/access_token
, and like the error says it must match theredirect_uri
value on the previous call to start theoauth/dialog
endpoint.The problem with this that you and I both have is that we’re using the FB JS SDK and the
FB.login(...)
method to authenticate/login. This process sets aredirect_uri
(that we don’t have access to and cannot override) within the popup.I’m not sure there is a solution at this time. Facebook provides a broken JS SDK and its docs are misleading.
zigzags answer is correct, it has to do with the redirect_uri.. I was able to overcome this issue using the manual-flow. Basically you have to create the oauth url manually and open it, the following code worked for me:
Hope this helps!