Google API PHP Login retreving Auth Error












1















I am trying to implement a log in via Google on my application to access some information and controle some features of the user Youtube channel but it is returning error all the time, saying that the user is not authenticated.



That's my code.



<?php
session_start();
ini_set('display_errors', 1);
error_reporting(E_ALL);

require_once 'vendor/autoload.php';

$OAUTH2_CLIENT_ID = '-----------------------------------------';
$OAUTH2_CLIENT_SECRET = '-----------------';
$REDIRECT = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$APPNAME = "Test";

try{
$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes([
'https://www.googleapis.com/auth/youtube.force-ssl',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/plus.me',
'https://www.googleapis.com/auth/userinfo.profile'
]);
$client->setRedirectUri($REDIRECT);
$client->setApplicationName($APPNAME);
$client->setDeveloperKey('---------------------');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
}
catch(Exception $e){
die('1: '.$e);
}

$youtube = new Google_Service_YouTube($client);
$google_oauth = new Google_Service_Oauth2($client);
$plusAuth = new Google_Service_Plus($client);

if (isset($_GET['code'])) {
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
die('The session state did not match.');
}

try{
$client->authenticate($_GET['code']);

$user_email = $google_oauth->userinfo->get()->email;
$user_name = $plusAuth->people->get('me')->displayName;
}
catch(Exception $e){
die('2: '.$e);
}

// Code in between

$_SESSION['token'] = $client->getAccessToken();
}

if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
//echo "Access Token: " . json_encode($_SESSION['token']);

header('Location: /main.php');
}


if ($client->getAccessToken()) {
$_SESSION['token'] = $client->getAccessToken();
}
else{
$state = mt_rand();
$client->setState($state);
$_SESSION['state'] = $state;

header('Location: '.$client->createAuthUrl());
}
?>


And I am getting this error:



2: Google_Service_Exception: { "error": { "code": 401, "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "errors": [ { "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "domain": "global", "reason": "unauthorized" } ], "status": "UNAUTHENTICATED" } }


I can't find the reason to the login flow is failing, but when it tries to use the properties of the Google Service Class's it always says that I am not authenticated.



If you can help me I am very welcome.










share|improve this question

























  • Which one of the API calls is throwing the Exception? I'm not seeing any of the numbered prefixes in your quoted error message.

    – Ro Achterberg
    Nov 22 '18 at 20:00













  • Edited now. It is the second one.

    – Ricardo Pereira
    Nov 22 '18 at 20:07











  • I'd start by checking what value $_GET['code'] holds. Please note that isset() does not check for the existence of a value, it would merely report that the 'code' parameter was passed.

    – Ro Achterberg
    Nov 22 '18 at 20:37











  • 4/nQBF4vLSGmzkPjGW-SqqkgUWRSM8HJHU8KtFRex0yrEMvjJOUijKya-D4Gzth2hDdV_lDh4KR3gnGPhO6dCITsE2 (it has the correct code passed by the authentification)

    – Ricardo Pereira
    Nov 22 '18 at 20:52








  • 1





    Found the error. Stupid as always... The redirect uri (http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]) was returning the parameters and when tries to authenticate it was returning "redirect uri mismatch"

    – Ricardo Pereira
    Nov 22 '18 at 21:21
















1















I am trying to implement a log in via Google on my application to access some information and controle some features of the user Youtube channel but it is returning error all the time, saying that the user is not authenticated.



That's my code.



<?php
session_start();
ini_set('display_errors', 1);
error_reporting(E_ALL);

require_once 'vendor/autoload.php';

$OAUTH2_CLIENT_ID = '-----------------------------------------';
$OAUTH2_CLIENT_SECRET = '-----------------';
$REDIRECT = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$APPNAME = "Test";

try{
$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes([
'https://www.googleapis.com/auth/youtube.force-ssl',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/plus.me',
'https://www.googleapis.com/auth/userinfo.profile'
]);
$client->setRedirectUri($REDIRECT);
$client->setApplicationName($APPNAME);
$client->setDeveloperKey('---------------------');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
}
catch(Exception $e){
die('1: '.$e);
}

$youtube = new Google_Service_YouTube($client);
$google_oauth = new Google_Service_Oauth2($client);
$plusAuth = new Google_Service_Plus($client);

if (isset($_GET['code'])) {
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
die('The session state did not match.');
}

try{
$client->authenticate($_GET['code']);

$user_email = $google_oauth->userinfo->get()->email;
$user_name = $plusAuth->people->get('me')->displayName;
}
catch(Exception $e){
die('2: '.$e);
}

// Code in between

$_SESSION['token'] = $client->getAccessToken();
}

if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
//echo "Access Token: " . json_encode($_SESSION['token']);

header('Location: /main.php');
}


if ($client->getAccessToken()) {
$_SESSION['token'] = $client->getAccessToken();
}
else{
$state = mt_rand();
$client->setState($state);
$_SESSION['state'] = $state;

header('Location: '.$client->createAuthUrl());
}
?>


And I am getting this error:



2: Google_Service_Exception: { "error": { "code": 401, "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "errors": [ { "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "domain": "global", "reason": "unauthorized" } ], "status": "UNAUTHENTICATED" } }


I can't find the reason to the login flow is failing, but when it tries to use the properties of the Google Service Class's it always says that I am not authenticated.



If you can help me I am very welcome.










share|improve this question

























  • Which one of the API calls is throwing the Exception? I'm not seeing any of the numbered prefixes in your quoted error message.

    – Ro Achterberg
    Nov 22 '18 at 20:00













  • Edited now. It is the second one.

    – Ricardo Pereira
    Nov 22 '18 at 20:07











  • I'd start by checking what value $_GET['code'] holds. Please note that isset() does not check for the existence of a value, it would merely report that the 'code' parameter was passed.

    – Ro Achterberg
    Nov 22 '18 at 20:37











  • 4/nQBF4vLSGmzkPjGW-SqqkgUWRSM8HJHU8KtFRex0yrEMvjJOUijKya-D4Gzth2hDdV_lDh4KR3gnGPhO6dCITsE2 (it has the correct code passed by the authentification)

    – Ricardo Pereira
    Nov 22 '18 at 20:52








  • 1





    Found the error. Stupid as always... The redirect uri (http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]) was returning the parameters and when tries to authenticate it was returning "redirect uri mismatch"

    – Ricardo Pereira
    Nov 22 '18 at 21:21














1












1








1








I am trying to implement a log in via Google on my application to access some information and controle some features of the user Youtube channel but it is returning error all the time, saying that the user is not authenticated.



That's my code.



<?php
session_start();
ini_set('display_errors', 1);
error_reporting(E_ALL);

require_once 'vendor/autoload.php';

$OAUTH2_CLIENT_ID = '-----------------------------------------';
$OAUTH2_CLIENT_SECRET = '-----------------';
$REDIRECT = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$APPNAME = "Test";

try{
$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes([
'https://www.googleapis.com/auth/youtube.force-ssl',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/plus.me',
'https://www.googleapis.com/auth/userinfo.profile'
]);
$client->setRedirectUri($REDIRECT);
$client->setApplicationName($APPNAME);
$client->setDeveloperKey('---------------------');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
}
catch(Exception $e){
die('1: '.$e);
}

$youtube = new Google_Service_YouTube($client);
$google_oauth = new Google_Service_Oauth2($client);
$plusAuth = new Google_Service_Plus($client);

if (isset($_GET['code'])) {
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
die('The session state did not match.');
}

try{
$client->authenticate($_GET['code']);

$user_email = $google_oauth->userinfo->get()->email;
$user_name = $plusAuth->people->get('me')->displayName;
}
catch(Exception $e){
die('2: '.$e);
}

// Code in between

$_SESSION['token'] = $client->getAccessToken();
}

if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
//echo "Access Token: " . json_encode($_SESSION['token']);

header('Location: /main.php');
}


if ($client->getAccessToken()) {
$_SESSION['token'] = $client->getAccessToken();
}
else{
$state = mt_rand();
$client->setState($state);
$_SESSION['state'] = $state;

header('Location: '.$client->createAuthUrl());
}
?>


And I am getting this error:



2: Google_Service_Exception: { "error": { "code": 401, "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "errors": [ { "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "domain": "global", "reason": "unauthorized" } ], "status": "UNAUTHENTICATED" } }


I can't find the reason to the login flow is failing, but when it tries to use the properties of the Google Service Class's it always says that I am not authenticated.



If you can help me I am very welcome.










share|improve this question
















I am trying to implement a log in via Google on my application to access some information and controle some features of the user Youtube channel but it is returning error all the time, saying that the user is not authenticated.



That's my code.



<?php
session_start();
ini_set('display_errors', 1);
error_reporting(E_ALL);

require_once 'vendor/autoload.php';

$OAUTH2_CLIENT_ID = '-----------------------------------------';
$OAUTH2_CLIENT_SECRET = '-----------------';
$REDIRECT = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$APPNAME = "Test";

try{
$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes([
'https://www.googleapis.com/auth/youtube.force-ssl',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/plus.me',
'https://www.googleapis.com/auth/userinfo.profile'
]);
$client->setRedirectUri($REDIRECT);
$client->setApplicationName($APPNAME);
$client->setDeveloperKey('---------------------');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
}
catch(Exception $e){
die('1: '.$e);
}

$youtube = new Google_Service_YouTube($client);
$google_oauth = new Google_Service_Oauth2($client);
$plusAuth = new Google_Service_Plus($client);

if (isset($_GET['code'])) {
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
die('The session state did not match.');
}

try{
$client->authenticate($_GET['code']);

$user_email = $google_oauth->userinfo->get()->email;
$user_name = $plusAuth->people->get('me')->displayName;
}
catch(Exception $e){
die('2: '.$e);
}

// Code in between

$_SESSION['token'] = $client->getAccessToken();
}

if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
//echo "Access Token: " . json_encode($_SESSION['token']);

header('Location: /main.php');
}


if ($client->getAccessToken()) {
$_SESSION['token'] = $client->getAccessToken();
}
else{
$state = mt_rand();
$client->setState($state);
$_SESSION['state'] = $state;

header('Location: '.$client->createAuthUrl());
}
?>


And I am getting this error:



2: Google_Service_Exception: { "error": { "code": 401, "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "errors": [ { "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "domain": "global", "reason": "unauthorized" } ], "status": "UNAUTHENTICATED" } }


I can't find the reason to the login flow is failing, but when it tries to use the properties of the Google Service Class's it always says that I am not authenticated.



If you can help me I am very welcome.







php oauth-2.0 google-api google-oauth google-plus






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 20:06







Ricardo Pereira

















asked Nov 22 '18 at 19:52









Ricardo PereiraRicardo Pereira

185




185













  • Which one of the API calls is throwing the Exception? I'm not seeing any of the numbered prefixes in your quoted error message.

    – Ro Achterberg
    Nov 22 '18 at 20:00













  • Edited now. It is the second one.

    – Ricardo Pereira
    Nov 22 '18 at 20:07











  • I'd start by checking what value $_GET['code'] holds. Please note that isset() does not check for the existence of a value, it would merely report that the 'code' parameter was passed.

    – Ro Achterberg
    Nov 22 '18 at 20:37











  • 4/nQBF4vLSGmzkPjGW-SqqkgUWRSM8HJHU8KtFRex0yrEMvjJOUijKya-D4Gzth2hDdV_lDh4KR3gnGPhO6dCITsE2 (it has the correct code passed by the authentification)

    – Ricardo Pereira
    Nov 22 '18 at 20:52








  • 1





    Found the error. Stupid as always... The redirect uri (http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]) was returning the parameters and when tries to authenticate it was returning "redirect uri mismatch"

    – Ricardo Pereira
    Nov 22 '18 at 21:21



















  • Which one of the API calls is throwing the Exception? I'm not seeing any of the numbered prefixes in your quoted error message.

    – Ro Achterberg
    Nov 22 '18 at 20:00













  • Edited now. It is the second one.

    – Ricardo Pereira
    Nov 22 '18 at 20:07











  • I'd start by checking what value $_GET['code'] holds. Please note that isset() does not check for the existence of a value, it would merely report that the 'code' parameter was passed.

    – Ro Achterberg
    Nov 22 '18 at 20:37











  • 4/nQBF4vLSGmzkPjGW-SqqkgUWRSM8HJHU8KtFRex0yrEMvjJOUijKya-D4Gzth2hDdV_lDh4KR3gnGPhO6dCITsE2 (it has the correct code passed by the authentification)

    – Ricardo Pereira
    Nov 22 '18 at 20:52








  • 1





    Found the error. Stupid as always... The redirect uri (http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]) was returning the parameters and when tries to authenticate it was returning "redirect uri mismatch"

    – Ricardo Pereira
    Nov 22 '18 at 21:21

















Which one of the API calls is throwing the Exception? I'm not seeing any of the numbered prefixes in your quoted error message.

– Ro Achterberg
Nov 22 '18 at 20:00







Which one of the API calls is throwing the Exception? I'm not seeing any of the numbered prefixes in your quoted error message.

– Ro Achterberg
Nov 22 '18 at 20:00















Edited now. It is the second one.

– Ricardo Pereira
Nov 22 '18 at 20:07





Edited now. It is the second one.

– Ricardo Pereira
Nov 22 '18 at 20:07













I'd start by checking what value $_GET['code'] holds. Please note that isset() does not check for the existence of a value, it would merely report that the 'code' parameter was passed.

– Ro Achterberg
Nov 22 '18 at 20:37





I'd start by checking what value $_GET['code'] holds. Please note that isset() does not check for the existence of a value, it would merely report that the 'code' parameter was passed.

– Ro Achterberg
Nov 22 '18 at 20:37













4/nQBF4vLSGmzkPjGW-SqqkgUWRSM8HJHU8KtFRex0yrEMvjJOUijKya-D4Gzth2hDdV_lDh4KR3gnGPhO6dCITsE2 (it has the correct code passed by the authentification)

– Ricardo Pereira
Nov 22 '18 at 20:52







4/nQBF4vLSGmzkPjGW-SqqkgUWRSM8HJHU8KtFRex0yrEMvjJOUijKya-D4Gzth2hDdV_lDh4KR3gnGPhO6dCITsE2 (it has the correct code passed by the authentification)

– Ricardo Pereira
Nov 22 '18 at 20:52






1




1





Found the error. Stupid as always... The redirect uri (http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]) was returning the parameters and when tries to authenticate it was returning "redirect uri mismatch"

– Ricardo Pereira
Nov 22 '18 at 21:21





Found the error. Stupid as always... The redirect uri (http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]) was returning the parameters and when tries to authenticate it was returning "redirect uri mismatch"

– Ricardo Pereira
Nov 22 '18 at 21:21












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437327%2fgoogle-api-php-login-retreving-auth-error%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437327%2fgoogle-api-php-login-retreving-auth-error%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'