php - google oauth authentication -
i trying pass authorization data google plus data variable keeps showing value of "moved temporarily" doing wrong?
<?php session_start(); $client_id = ''; $client_secret = ''; $api_key = ''; $redirect_uri = 'http://localhost:8888/oauth'; $scope = "https://www.googleapis.com/auth/plus.me"; if (!isset($_request['code'])) { $login_path = "https://accounts.google.com/o/oauth2/auth?"; $login_path .= "redirect_uri=" . $redirect_uri; $login_path .= "&response_type=code"; $login_path .= "&client_id=" . $client_id; $login_path .= "&approval_prompt=force"; $login_path .= "&scope=" . $scope; $login_path .= "&access_type=offline"; echo "<a href='" . $login_path . "'>login</a>"; } else { $ch = curl_init(); curl_setopt($ch, curlopt_url, "https://accounts.google.com/o/oauth2/auth"); curl_setopt($ch, curlopt_post, true); // option set true response // doesnot printed , stored directly in // variable curl_setopt($ch, curlopt_returntransfer, true); $post_params = "code=" . $_request['code'] . "&"; $post_params = "redirect_uri=" . $redirect_uri . "&"; $post_params .= "client_id=" . $client_id . "&"; $post_params .= "scope=" . $scope . "&"; $post_params .= "client_secret=" . $client_secret . "&"; $post_params .= "grant_type=authorization_code&"; $post_params .= "response_type=code"; curl_setopt($ch, curlopt_postfields, $post_params); $data = curl_exec($ch); curl_close($ch); //commented since data showing empty //$data = json_decode($data); print '<pre>'; print $data; print '</pre>'; }
update: else having same problem figured out. here working code.
$client_id = ''; $client_secret = ''; $api_key = ''; $redirect_uri = 'http://localhost:8888/oauth'; $scope = "https://www.googleapis.com/auth/plus.me"; $api_call = "https://www.googleapis.com/plus/v1/people/me?access_token="; if (!isset($_request['code'])) { $login_path = "https://accounts.google.com/o/oauth2/auth?"; $login_path .= "redirect_uri=" . $redirect_uri; $login_path .= "&response_type=code"; $login_path .= "&client_id=" . $client_id; $login_path .= "&approval_prompt=force"; $login_path .= "&scope=" . $scope; $login_path .= "&access_type=offline"; echo "<a href='" . $login_path . "'>login</a>"; } else { $ch = curl_init(); curl_setopt($ch, curlopt_url, "https://accounts.google.com/o/oauth2/token"); curl_setopt($ch, curlopt_post, true); // option set true response // doesnot printed , stored directly in // variable curl_setopt($ch, curlopt_returntransfer, true); $post_params = "code=" . $_request['code'] . "&"; $post_params .= "redirect_uri=" . $redirect_uri . "&"; $post_params .= "client_id=" . $client_id . "&"; $post_params .= "scope=" . $scope . "&"; $post_params .= "client_secret=" . $client_secret . "&"; $post_params .= "grant_type=authorization_code&"; curl_setopt($ch, curlopt_postfields, $post_params); $data = curl_exec($ch); curl_close($ch); $data = json_decode($data); $access_token = $data->access_token; $refresh_token = $data->refresh_token; // end of oauth call google plus api. not $api_call holds request uri value $call = $api_call . $access_token; $ch = curl_init(); curl_setopt($ch, curlopt_url, $call); curl_setopt($ch, curlopt_returntransfer, true); $out = curl_exec($ch); echo '<pre>'; print_r($out); echo '</pre>';
}
Comments
Post a Comment