FromDK / google-api-php-client

Automatically exported from code.google.com/p/google-api-php-client
Apache License 2.0
0 stars 0 forks source link

OAuth2 approval_prompt=auto #45

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
According to this blog entry 
(http://googlecode.blogspot.com/2011/10/upcoming-changes-to-oauth-20-endpoint.ht
ml), if you do not specify a value for approval_prompt or if you set it to 
"auto", the service should not ask the user to approve more than once.

Using the PHP client, doing so gives me:
Error fetching OAuth2 access token, message: 'redirect_uri_mismatch'

Original issue reported on code.google.com by nash...@gmail.com on 12 Dec 2011 at 11:59

GoogleCodeExporter commented 8 years ago
You'll need to use the latest version of the client library on trunk 
http://code.google.com/p/google-api-php-client/source/checkout.

New auto approval functionality:
$client->setApprovalPrompt('auto');

New 'online' access type:
$client->setAccessType('online');

As for the redirect_uri_mismatch error, you need register your redirect uri 
within the google api console.

Example using auto approval:
$client = new apiClient();
$client->setApplicationName("Google+ PHP Starter Application");
$client->setApprovalPrompt('auto');

// Visit https://code.google.com/apis/console?api=plus to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('insert_your_oauth2_client_id');
$client->setClientSecret('insert_your_oauth2_client_secret');
$client->setRedirectUri('insert_your_oauth2_redirect_uri');
$client->setDeveloperKey('insert_your_developer_key');

$plus = new apiPlusService($client);

if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) {
  $me = $plus->people->get('me');
  print "Your Profile: <pre>" . print_r($me, true) . "</pre>";

  $_SESSION['token'] = $client->getAccessToken();
} else {
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";
}

Original comment by chirags@google.com on 12 Dec 2011 at 5:03

GoogleCodeExporter commented 8 years ago

Original comment by chirags@google.com on 14 Dec 2011 at 10:17