Why isn't my Google OAuth 2.0 Working? - php

I've been working on a small script to grab YouTube channel data and my Google OAuth 2.0 isn't working.
$validate = "https://accounts.google.com/o/oauth2/auth?client_id=242340718758-65veqhhdjfl21qc2klkfhbcb19rre8li.apps.googleusercontent.com&redirect_uri=http://conor1998.web44.net/php/oauth.php&scope=https://www.googleapis.com/auth/yt-analytics.readonly&response_type=code&access_type=offline";
echo "<a href='$validate'>Login with Google for advanced analytics</a>";
if(isset($_GET['code'])) {
// try to get an access token
$code = $_GET['code'];
$url = 'https://accounts.google.com/o/oauth2/token?code='.$code.'&client_id=242340718758-65veqhhdjfl21qc2klkfhbcb19rre8li.apps.googleusercontent.com&client_secret={secret}&redirect_uri=http://conor1998.web44.net/php/oauth.php&grant_type=authorization_code';
$url = urlencode($url);
header('Location: $url');
}
$response = file_get_contents($url);
$response = json_decode($response);
$channel_data = file_get_contents('https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel==mine&start-date=2014-08-01&end-date=2014-09-01&metrics=views&key=AIzaSyDTxvTLWXStUrhzgCDptVUG4dGBCpyL9MY?alt=json');
$channel_data = json_decode($channel_data, true);
echo "<br />";
var_dump($channel_data);
echo "<br />";
I have no idea why it doesn't work. I feel it's mainly due to my goal of trying to get the authentication token for the user so i can grab their YouTube data. Any help would be appreciated

The code you receive from the auth website is not the access token! You have to exchange it for refresh and access tokens (see #4).
You have to perform a POST request to https://accounts.google.com/o/oauth2/token in order to get your tokens. It is not working via GET (as you can see when clicking the link).

Related

Get only specific code

In my site user get their own secret code with access token and submit That code in my site and from this all code I only want to take access token with the help of php...
Check this ScreenShootss.jpg
Example of access token {"session_key":"5.TVJlCXvhgqdhpA.1497109242.26-100007001746590","uid":100007001746590,"secret":"80cc3dc2ba89e635dcf84b41d6efcc38","access_token":"EAAAAAYsX7TsBAEa6qMaCj1qCgnOKFHBcfu76C6PrUdK1LnIh39jmabZAdVWmQLO3Ol64ZCXY4388DBfUwksxONGXE5dUY0mK9M07aszl5Qvs8ccqQ39xLEsK2gc1RUJQ0Kqy1ror7R8EPHZCX6pOzX0o4oQAJ1kOq8Oz0n0GysK64ebCsDEokwG36j1awnYaDoJPvOn2AZDZD","machine_id":"-hI8WaLcNeEVUSPuxcEpmhQU","confirmed":true}
And I also try to separate access token by separating all this code but this actually not working
Here is my php script
if(isset($_POST['submit'])) {
$token2 = $_POST['token'];
if(preg_match("'access_token=(.*?)&expires_in='", $token2, $matches)){
$token = $matches[1];
}
else{
$token = $token2;
}
$extend = get_html("https://graph.facebook.com/me/permissions?access_token=" . $token);
How to do this and thanks in advance
please try to parse this json into an array using php, then you can collect the token attribute that you need and continue with your validation flow. There is a related link with this solution: How to convert JSON string to array
There is a basic example:
$your_submit_data = '{"session_key":"5.TVJlCXvhgqdhpA.1497109242.26-100007001746590","uid":100007001746590,"secret":"80cc3dc2ba89e635dcf84b41d6efcc38","access_token":"EAAAAAYsX7TsBAEa6qMaCj1qCgnOKFHBcfu76C6PrUdK1LnIh39jmabZAdVWmQLO3Ol64ZCXY4388DBfUwksxONGXE5dUY0mK9M07aszl5Qvs8ccqQ39xLEsK2gc1RUJQ0Kqy1ror7R8EPHZCX6pOzX0o4oQAJ1kOq8Oz0n0GysK64ebCsDEokwG36j1awnYaDoJPvOn2AZDZD","machine_id":"-hI8WaLcNeEVUSPuxcEpmhQU","confirmed":true}';
$your_array = json_decode($your_submit_data, TRUE);
echo($your_array['access_token']);
Best,
You can use this code to get only access_token from JSON code
if(isset($_POST['submit'])) {
$token2 = $_POST['token'];
$obj = json_decode($token2);
echo "Access Token is: <br />". $obj->{'access_token'}; // You can remove it or use this value
$token = $obj->{'access_token'};
$extend = get_html("https://graph.facebook.com/me/permissions?access_token=" . $token);
}

Instagram followers are not showing?

I'm using this code to get followers count!
<?php
$api_key = '------';
$user_id = '------';
$data = #file_get_contents("https://api.instagram.com/v1/users/$user_id/?client_id=$api_key");
$data = json_decode($data, true);
echo '<pre/>';
print_r($data);
echo $data['data']['counts']['followed_by'];
?>
It's failing because you can't use a client ID to access that function of the API.
You need to get an Access Token by sending a user to the authorisation url. Read about it in the Instagram Docs.
After that, you make the request to /users using the Access Token from before.

WP-API oAuth Server to Server Flow

I'm a little confused about the flow I need to use if trying to connect to a remote Wordpress WP-API from another server (in this case another WP instance on the same server). I am using the PECL oAuth package, and most of the code I gathered up from the docs at https://secure.php.net/manual/en/class.oauth.php.
This is tied into a wordpress save hook like this, so every time someone saves a post on SITE A, it will attempt to send some info over to SITE B:
add_action( 'save_post', 'CrossPollinate_Save',10,3);
Inside CrossPollinate_Save is this:
$client_key = "....";
$client_secret = "....";
$request_token_endpoint = "http://..../oauth1/request";
$authorize_endpoint = "http://..../oauth1/authorize";
$access_endpoint = "http://..../oauth1/access";
$callback = $_SERVER['REQUEST_URI'];
$request_token = ""; //populated later
$request_token_secret = ""; //populated later
//STEP 1
$oauth = new OAuth($client_key, $client_secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
$request_token_info = $oauth->getRequestToken($request_token_endpoint);
if(!empty($request_token_info)) {
logToFile("Response from getRequestToken", $request_token_info);
} else {
logToFile("Failed fetching request token: ", $oauth->getLastResponse());
}
$request_token = $request_token_info["oauth_token"];
$request_token_secret = $request_token_info["oauth_token_secret"];
logToFile("request_token is: ", $request_token);
logToFile("request_token_secret is: ", $request_token_secret);
//STEP 2
$oauth->setToken($request_token, $request_token_secret);
$access_token_info = $oauth->getAccessToken($authorize_endpoint."?oauth_callback=".$callback);
if(!empty($access_token_info)) {
logToFile("Got access token! ", $access_token_info);
} else {
logToFile("Failed fetching access token: " . $oauth->getLastResponse());
}
I get an oauth_token and a oauth_token_secret from "step 1", great, that part works! When step 2 fires it ends up returning with a response that contains the markup for the login page. How do I tell oAuth to skip that step and just send the access token back to the redirect page?
I don't think there's a way around having to do the full 3-legged auth. I've found nothing to the contrary anyway and have just accepted that I'll have to do the redirect after getting the initial tokens.

How to fetch Linkedin feeds from my company page without user having to authenticate

I am trying to display my linkedin company page updates on my site as indicated here.
https://developer.linkedin.com/reading-company-shares
I downloaded the source from here
http://thinkdiff.net/linkedin/integrate-linkedin-api-in-your-site/
and the added the following method in linkedin.php
function company($parameters)
{
//$search_url = $this->base_url."/v1/companies/{$parameters}/updates";
//$search_url = $this->base_url . "/v1/people-search?keywords=facebook";
$search_url = "https://api.linkedin.com/v1/companies/918677/updates?";
echo "Performing search for: " . $parameters . "<br />";
echo "Search URL: $search_url <br />";
$request = OAuthRequest::from_consumer_and_token($this->consumer, $this->access_token, "GET", $search_url);
$request->sign_request($this->signature_method, $this->consumer, $this->access_token);
$auth_header = $request->to_header("https://api.linkedin.com");
print_r($auth_header);
$response = $this->httpRequest($search_url, $auth_header, "GET");
return $response;
}
I am unable to make it work. Also I am trying to show them without the user having to login.
Is it possible ? Any same code or tutorial will be helpful.
Thanks..
First you have to get user token of linkedin user Use Socialite or REST API package for oauth.
Once you having user token it is pretty simple to fetch Company list under admin user
$token = 'uflefjefheilhfbwrfliehbwfeklfw'; // user token
$api_url = "https://api.linkedin.com/v1/companies?oauth2_access_token=".$token."&format=json&is-company-admin=true";
$pages = json_decode(file_get_contents($api_url));
You have get $pages json array of list of company profile.

Exchange Access Token does not return output sometime in facebook graph api

I am using graph api to exchange access token in one of my application, but sometime its returns blank output.
I am using this code snipet
$graph_url = "https://graph.facebook.com/oauth/access_token?client_id=" .$app_id."&client_secret=" .$app_secret."&grant_type=fb_exchange_token&fb_exchange_token=".$user_access_token;
$response = #file_get_contents($graph_url);
$response_arr = explode('&',$response);
$exchanged_user_access_token = explode('=',$response_arr[0]);
return $exchanged_user_access_token[1];
It will return black response sometime
Any help will appreciate.
Thank you
Yes, you can solve this issue. Use the following code
<?php
$graph_url = "https://graph.facebook.com/oauth/access_token?client_id=" .$app_id."&client_secret=" .$app_secret."&grant_type=fb_exchange_token&fb_exchange_token=".$user_access_token;
$response = #file_get_contents($graph_url);
parse_str($response,$output);
$extended_access_token = $output['access_token'];
?>
I hope that this will help you

Categories