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);
}
Related
I'm trying to implement the Patreon API on a project and I've hit a roadblock. I'm able to sign in and receive an authorization code after being redirected back to my site, but for some reason the array that is supposed to have the access and refresh tokens in them is null.
Im using the patreon package via composer https://packagist.org/packages/patreon/patreon
So the top of my code seems to be working fine. However, in case I'm completely blind and it is the issue I'm including it.
$client_id = 'xxxxxxxxxx'; // Replace with your data
$client_secret = 'xxxxxxxxx'; // Replace with your data
$redirect_uri = "http://localhost/Final%20API/thankyou.php";
// Generate the oAuth url
$href = 'https://www.patreon.com/oauth2/authorize?response_type=code&client_id=' . $client_id . '&redirect_uri=' . urlencode($redirect_uri);
$state = array();
$state['final_page'] = 'http://localhost/Final%20API/';
$state_parameters = '&state=' . urlencode( base64_encode( json_encode( $state ) ) );
// Append it to the url
$href .= $state_parameters;
$scope_parameters = '&scope=identity%20identity'.urlencode('[email]');
$href .= $scope_parameters;
echo 'Click here to login via Patreon';
Notice: Trying to access array offset on value of type null in C:\MAMP\htdocs\patreon3.php on line 69
Notice: Trying to access array offset on value of type null in C:\MAMP\htdocs\patreon3.php on line 70
I get the code= returned so i'm trying to use it to get my tokens, but I'm coming up empty-handed and I don't know what I'm doing wrong here.
if ( $_GET['code'] != '' ) {
$code = $_GET['code'];
$oauth_client = new OAuth($client_id, $client_secret);
$tokens = $oauth_client->get_tokens($code, $redirect_uri);
$access_token = $tokens['access_token'];
$refresh_token = $tokens['refresh_token'];
Am I correct in thinking that the sticking point is that if statement? If so, what do I have to do to straighten it out?
Can you check what is inside $tokens?
After this line
$tokens = $oauth_client->get_tokens($code, $redirect_uri);
Add this
var_dump($tokens);
You propably get some error
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.
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.
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).
I'm trying to access date using the Springpad API.
If I use the following link:
http://springpadit.com/api/blocks/all?limit=1&text=HARRY+POTTER
I have no problem getting 2 recent results with the search term "J K Rowlings'.
I wrote the following code to do the same thing after authorizing my server:
$api_url = "http://springpadit.com/api/";
$query = $_GET['query'];
$param = array('limit'=>1, 'text'=>$query);
$temp = http_build_query($param,"","&");
$url = $api_url."blocks/all?".$temp;
session_start();
// In state=1 the next request should include an oauth_token.
// If it doesn't go back to 0
if(!isset($_GET['oauth_token']) && $_SESSION['state']==1) $_SESSION['state'] = 0;
try {
$oauth = new OAuth($conskey,$conssec,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
$oauth->enableDebug();
if(!isset($_GET['oauth_token']) && !$_SESSION['state']) {
$request_token_info = $oauth->getRequestToken($req_url);
$_SESSION['secret'] = $request_token_info['oauth_token_secret'];
$_SESSION['state'] = 1;
header('Location: '.$authurl.'?oauth_token='.$request_token_info['oauth_token'].'&oauth_token_secret='.$_SESSION['secret']);
exit;
} else if($_SESSION['state']==1) {
$oauth->setToken($_GET['oauth_token'],$_SESSION['secret']);
$access_token_info = $oauth->getAccessToken($acc_url);
$_SESSION['state'] = 2;
$_SESSION['token'] = $access_token_info['oauth_token'];
$_SESSION['secret'] = $access_token_info['oauth_token_secret'];
}
$oauth->setToken($_SESSION['token'],$_SESSION['secret']);
$oauth->fetch($url);
$json = json_decode($oauth->getLastResponse(),true);
print_r($json['blocks']);
} catch(OAuthException $E) {
print_r($E);
}
This should let you create the same query and retrieve the data by using the following link:
http://xrefpro.com/CRM/index.php?query=HARRY+POTTER
All I get is an empty array for my results. What am I doing wrong here??
The springpad api call "/api/blocks/all" is a global search across all users public data. It does not require auth. There does seem to be a bug with the api that causes that search to not work if you are logged in. You can test this by logging out of springpad and hitting
http://springpadit.com/api/blocks/all
and then try it when logged in.
I imagine what is happening is it is trying to find a block with that text in your own account. I am an employee at springpadit.com, we will look into fixing that bug with global queries. For now though, don't bother with oauth for a global query.
If you want to search your own account, use oauth and query
http://springpadit.com/api/users/me/blocks?limit=1&text=Thor
The response won't have a "blocks" node, so just change the print to
print_r($json);