Instagram followers are not showing? - php

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.

Related

PHP and Google Drive authentication frustration

This is driving me mental.
I have a web application and an associated Google Account. I want the web application to use this Google drive and this google drive ONLY...PERMANENTLY.
I am using google/apiclient:^2.0
I have set up a OAuth 2.0 client ID and downloaded the JSON file.
I have this:
$this->client = new \Google_Client();
$this->client->setClientId('blahblahblah.apps.googleusercontent.com');
$this->client->setAuthConfig(base_path() . '/resources/assets/client_secret.json');
$this->client->setApplicationName('My Web App');
$this->client->setRedirectUri('somewhere');
$this->client->setScopes('https://www.googleapis.com/auth/drive');
return $this->client;
Now when I run...
$authUrl = $this->client->createAuthUrl();
echo 'Go';
And authenticate I get a Code...
Now my question is...what do I do with that code?
I've tried...$this->client->authenticate('code here');
and also $accessToken = $client->fetchAccessTokenWithAuthCode('code here);
I keep getting either dailyLimitExceededUnreg or Invalid token format
I'm really confused and frustrated with the Google authentication API and the docs seem way out of date.
Any hints in the right direction would be amazing.
Thanks
I did something like this a couple of years ago and also had some difficulties with the documentation.
I went through the code to find it for you. I used this for Gmail contact list but the procedure looks the same. I'll try to explain the process I went through and I think it should help you out.
This it the part where you are. You get the code Google send you and just save it in a Session Variable
if (isset($_GET['code'])) {
$auth_code = $_GET["code"];
$_SESSION['google_code'] = $auth_code;
}
Now you will have to post to oauth2 to authenticate and get your acesstoken
$auth_code = $_SESSION['google_code'];
$max_results = 300;
$fields=array(
'code'=> urlencode($auth_code),
'client_id'=> urlencode($google_client_id),
'client_secret'=> urlencode($google_client_secret),
'redirect_uri'=> urlencode($google_redirect_uri),
'grant_type'=> urlencode('authorization_code')
);
$post = '';
foreach($fields as $key=>$value)
{
$post .= $key.'='.$value.'&';
}
$post = rtrim($post,'&');
$result = curl('https://accounts.google.com/o/oauth2/token',$post);
$response = json_decode($result);
$accesstoken = $response->access_token;
With the token you'll be able to curl Google Drive's endpoint and get your results
$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&alt=json&v=3.0&oauth_token='.$accesstoken;
$xmlresponse = curl($url);
To get the access token you need the following in you "somewhere" route:
$client->authenticate($_GET['code']);
$access_token = $client->getAccessToken();
the access token is used to log you into your google drive
to work with google drive you need to instantiate Google_Service_Drive
$drive = new Google_Service_Drive($client);
$files = $drive->files->listFiles(array())->getItems();
Note: access tokens are a form of user+password that expire over time so you need to fetch new ones if they expire

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);
}

Why isn't my Google OAuth 2.0 Working?

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).

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

$me = $facebook->api('/me'); returns empty string

This piece of code used to work as expected, but now it seems i can't retrieve the user name the same way:
$uid = $facebook->getUser(); // returns the facebook id of the user
$me = $facebook->api('/me'); // returns empty. why?
Due some facebook api updates i had to change the getSession from:
$session = $facebook->getUser();
to
$session = $facebook->getUser()>0;
So how to retrieve the user name in facebook?
So how to retrieve the user name in facebook?
What about this :
$pageContent = file_get_contents('http://graph.facebook.com/USERID');
$parsedJson = json_decode($pageContent);
echo $parsedJson->name;
Facebook use CURL for send request to server, maybe curl on your server not work or curl_exec is disabled

Categories