I have read the instructions here: https://github.com/hwi/HWIOAuthBundle/blob/master/Resources/doc/internals/response_object_and_paths.md
and have copied all the settings.
I am having trouble finding where I can get the response as shown in the instructions as:
/* #var $response \HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface */
var_dump(
$response->getEmail(),
$response->getProfilePicture()
);
I have overridden the ConnectController and have been playing around with it but I dont know how to access the $response given from facebook.
I have tried:
$response = $this->getResourceOwnerByName('facebook')
->getUserInformation($this->container->get('security.context')
->getToken()->getRawToken())->getResponse();
inside the connectAction() method but it doesnt seem to be working.
How can I access the response from facebook so I can grab the user profile data?
Also at what point in the life cycle of the Oautherization is this data sent?
Figured it out.
I can access the $response from my user provider.
In my particular case I have extended overiden the default FOSUserProvider and access the $response here:
AppBundle\Security\Core\User\FOSUBUserProvider::loadUserByOAuthUserResponse()
Related
I am working on a PHP-based web app that's to be used internally by my agency's 3 team members, to read campaign and ads data.
Ideally, for the sake of security, the tool would never have the ability to edit ads.
It's a comparatively simple tool, I think. So I opted for cURL requests rather than using the PHP Business SDK.
The App is in 'In Development' mode, and it's being access by a System User in Business Manager whose token permissions are:
read_insights
ads_read
pages_read_engagement
The System User has been given access to the following Assets:
The Ad Account. Permissions granted: View Performance
The Page. Permissions granted: View Page Performance
With those settings in place, the application is able to pull almost everything needed to view the info we require for our campaigns/ad sets/ads. I'm quite proud of it. But there's only one problem. I can't seem to get the destination URL for any ads through any API requests.
From what I understand, I need to query an ad's AdCreative.
With php cURL requests like this:
$access_token = "REDACTED";
$app_secret = "REDACTED";
$appsecret_proof = hash_hmac('sha256', $access_token, $app_secret);
$handle = curl_init();
$url = "https://graph.facebook.com/v7.0/23845023771530707?fields=object_story_spec,thumbnail_url,link_url,call_to_action_type&access_token=$access_token&appsecret_proof=$appsecret_proof";
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($handle);
curl_close($handle);
$result = json_decode($output,true);
echo "<pre>";print_r($result);echo "</pre>";
I get the following return:
Array
(
[object_story_spec] => Array
(
[page_id] => 104422504276761
)
[thumbnail_url] => https://external.xx.fbcdn.net/safe_image.php?d=AQCP_C2YI....Shortened here for clarity of this post
[id] => 23845023771530707
)
In short, only about half of the fields seem to populate. I really need to get the final destination URL of an ad. I do not get the link_url or call_to_action_type fields that I requested. :(
So, I'm wondering things like...
Am I going about this wrong? Is AdCreative the wrong way to query this info?
Am I somehow still lacking permissions?
Am I unable to get these fields because the App in still 'In Development'? Do I need to submit the App for Review?
Sorry there are several questions here rolled into one post.
The field object_story_spec in the creative object is only present if the ad created a new page-post on publish. It won't be present if the ad was created referencing an existing page-post.
Fortunately, the reference to the created/referenced page-post effective_object_story_id should exist on every ad-creative.
You can query the page-post via /{effective_object_story_id}?fields=... to find the data that would have otherwise existed in object_story_spec.
ad-creative: https://developers.facebook.com/docs/marketing-api/reference/ad-creative
page-post: https://developers.facebook.com/docs/graph-api/reference/page-post
I have a Page Tab facebook app. I want to post to users timeline from it.
After login on client side with javascript sdk (I use angularjs module for that Ciul/angular-facebook (sorry, cannot post github link here)):
https://gist.github.com/Sevavietl/7e363fdfd0e714a12a43
I retrieve access_token on server side and trying to post a carousel to the users feed:
https://gist.github.com/Sevavietl/cec5fa434837312adfd3
I have two problems:
While first call I get
Graph returned an error: (#210) Param id must be a page.
After browsing, I found that this can be caused by wrong token usage. Not user_access_token. But I login the user on the client side.
And for next calls I get
Graph returned an error: This authorization code has been used.
Again, after browsing, I found that token can be used only once in order to be OAuth compliant.
Please, advise me how to do this right?
Thank you in advance.
Best regards,
Vsevolod
After spending some time on this, I am writing my solution, as it was not really obvious, at least for me. But, actually, all info is present in the documentation.
Starting with problem number 2: And for next calls I get Graph returned an error: This authorization code has been used.
As it was mentioned in the comments, I indeed was confusing access_token with authorization code. As I understand in really simplified form, the mechanism is:
When you login with facebook-javascript-sdk it writes authorization code to cookies.
Then on the server side you can retrieve access_token from javaScriptHelper available in facebook-php-sdk. This helper has getAccessToken() method, which retrieves access_token using authorization code from cookies. This is the place where I was confused. I was trying to use getAccessToken() on every request. As requests were made with AJAX the authorization code was not changed, and as you can use it only once I was getting an error. The solution to this was pointed in many places on the Internet, but somehow I was able to misunderstand this.
Here is the code excerpt that uses sessions to store access_token, and uses getAccessToken() method only if access_token is not set in the session.
$fb = new Facebook([
'app_id' => $widget->app_id,
'app_secret' => $widget->app_secret,
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getJavaScriptHelper();
if ($request->session()->has('facebook_access_token')) {
$accessToken = $request->session()->get('facebook_access_token');
} else {
$accessToken = $helper->getAccessToken();
// OAuth 2.0 client handler
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
$request->session()->put('facebook_access_token', (string) $longLivedAccessToken);
$request->session()->save();
}
if ($accessToken) {
$fb->setDefaultAccessToken($accessToken);
} else {
die('No Access Token');
}
I use Laravel, so the session handling is not framework agnostic. This code is only for example, it is better to create service and move logic there.
Now. About the first problem: While first call I get Graph returned an error: (#210) Param id must be a page.
Again I was confusing two things '{user-id}/feed' and '{page-id}/feed'. My aim was to post a carousel with images and links. To create a carousel you have to provide child_attachments in the fields array. While you can send a Post like this to '{page-id}/feed', you cannot do so for '{user-id}/feed'. So at the moment you cannot post a carousel to users feed.
So when Graph Api was getting a Post data applicable for '{page-id}/feed', it was assuming that I have passed the {page-id}. And when getting the {user-id} instead, the Graph Api yelled back "(#210) Param id must be a page.".
I start to code in PHP and I am facing a problem, I used classic method to get User Profil Picture :
https://graph.facebook.com/mark/picture
But as you know, Facebook with little regard for small coders like me. They have cleared this easy method. Now I have to connect on their API to get a facebook profile picture.
I have used fil_get_content on the Mobile Facebook site to retrieve the profile image, but it asks for a captcha.
I am FORCED to go through their API, also I created a APP_ID and APP_SECRET but I do not know how. Despite all the tutorials I've tried these last two hours.
If someone would have a solution, it would be nice.
Thank you, good evening.
Use following Code to get your profile picture
$request = new FacebookRequest(
$session,
'GET',
'/me',
array(
'fields' => 'picture'
)
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
/* handle the result */
Now print your response using $response variable.
In a response you got a url.
Can i get ALL my posts(my Profile feed) using Graph API explorer?
I tried using https://graph.facebook.com//posts?limit=500
but it doesn't seem to give me everything(even increasing the limit doesn't work) which
means it given only a predefined number of posts.
Is there a way to get all posts by me from the time i joined Facebook(because i can get them by scrolling down on my Wall which means facebook has stored them).
Thanks in advance
Get Access Token
http://developers.facebook.com/tools/explorer
Then
https://graph.facebook.com/YOUR_ID/feed?access_token=FACEBOOK_ACCESS_TOKEN
This post might also be helpful:
How to show facebook feed messages from my site without access_token?
You do not need an access token!
$url = 'url_pulled_from_database';
$url = str_replace('http://www.', 'http://graph.', $url);
$get_id = file_get_contents($url);
$get_id = json_decode($get_id, true);
$fbID = $get_id['id'];
//THEN CALL THE FUNCTION
fb_parse_feed($fbID, $maxnumber);
I cant take credit for the fb_parse_feed function, but you can find it here
https://gist.github.com/banago/3864515
Facebook's real-time updates docs now say that you can get the feed for a page:
You can subscribe to the page's feed in the same way you subscribe to
a user's feed - the subscription topic should be 'user' and the
subscription field should be 'feed'
My understanding of this is that I need to post to the graph API to subscribe as such:
curl -d \
"object=user&fields=feed&callback_url=$CALLBACKURL&verify_token=$SECRET" \
"https://graph.facebook.com/$PAGEID/subscriptions?access_token=$OAUTHTOKEN"
(This is a bash script.) Here $CALLBACKURL is set up correctly following their example code. (At least I think it's correct -- I can successfully add subscriptions with it.)
The $OAUTHTOKEN is the one for my Facebook App.
And the $PAGEID is the facebook object id of the page I'd like to get realtime updates for.
When I do this, the call appears to work -- no error message. My callback gets called. But I certainly don't get notified when something happens on the page's feed.
So what's missing? Does the page need to install the app whose oauth token I'm using? Or do I somehow need to get an oauth token for the page itself by logging in as the page?
I do not know if this can help you but I'll tell you where I am for real-time update page feed:
(permissions : manage_page,offline_access,read_stream)
your application must be linked to the page (then install the application but not required to have a tab ex. create tab https://graph.facebook.com/PAGE_ID/tabs?app_id=APP_ID&method=POST&access_token=PAGE_ACCESS_TOKEN and delete tab
TAB_ID=PAGE_ID.'/tabs/app_'.APP_ID;
https://graph.facebook.com/TAB_ID?method=DELETE&access_token=PAGE_ACCESS_TOKEN)
function page_access_token($page_id,$access_token){
$page_token_url="https://graph.facebook.com/" .
$page_id . "?fields=access_token&" . $access_token;
$response = file_get_contents($page_token_url);
$resp_obj = json_decode($response,true);
$page_access_token = $resp_obj['access_token'];
return $page_access_token;
}
function page_tabs_create($page_id,$app_id,$page_access_token){
$page_settings_url = "https://graph.facebook.com/" .
$page_id . "/tabs?app_id=".$app_id."&method=POST&access_token=" . $page_access_token;
$response = file_get_contents($page_settings_url);
$resp_obj = json_decode($response,true);
return $resp_obj;
}
function page_tabs_delete($tab_id,$page_access_token){
$page_settings_url = "https://graph.facebook.com/".$tab_id."?method=DELETE&access_token=" . $page_access_token;
$response = file_get_contents($page_settings_url);
$resp_obj = json_decode($response,true);
return $resp_obj;
}
Subscription: to subscribe must be like a "user" and therefore object = user fields = feed but you have to add new fields because otherwise it receives the comments and likes the wall so you must add "status" in order to receive the articles posted (my problem is to get other content such as links I have managed to add "link" as fields but I am not receiving notifications when a link is posted)
$param = array ('access_token' => $ access_token,
'object' => 'user',
'fields' => 'feed, status, link'
'callback_url' => 'http:// ******** fbcallback.php'
'verify_token' =>'***********',
'include_values' => 'true');
POST
https://graph.facebook.com/APP_ID/subscriptions
my English is not very good but I hope it will help you, on my side I'm still looking for really all updates to the wall (links, video ...)
I have been researching on this and i am getting the pushes for pages too.
while subscribing make sure that object = user and fields = feed, status is present.
Make sure that application is added to your page, then only you will receive the update.
Response received from facebook is as below :-
{"entry":[{"id":"*******","uid":"*****","time":1332940650,"changed_fields":["status"]}],"object":"user"}
where **** is my pageId.
The above push is received on adding a new post , as you can see the changed_feild is status , but when we post on wall the changed_fields comes as feed. Below link helped me in receiving pushes for the page.
http://bugs.developers.facebook.net/show_bug.cgi?id=18048
Firstly, subscription to real-time update for page/feed doesn't work.
See: http://bugs.developers.facebook.net/show_bug.cgi?id=18048
Does the page need to install the app whose oauth token I'm using?
Yes
Or do I somehow need to get an oauth token for the page itself by logging in as the page?
If i correctly understand you, you need only app access_token
https://graph.facebook.com/oauth/access_token?client_id=your_app_id&client_secret=your_secret&grant_type=client_credentials
I first started reading the docs about real-time updates thinking that it could delivery the actual data. But, as I realized, that's not the purpose.
Real-time updates are just to tell you that something has changed, and once it happens, you can call the api with the conventional method.
Also, seems like there's an error on this part the subscription topic should be 'user' and the subscription field should be 'feed'. The subscription topic, actually, should be 'page'. (Have you thought the same?)
And by subscribe on page feed as the same as user feed, means that you can get update notifications, not the data itself.
Not sure if this is a common mistake, but I'm posting it just in case.
--
Currently I'm working on a data mining tool that will need real-time updates. But first, I'm focused on the data itself, so I can't post any examples yet. (I'll edit this answer when implementing this part)
What I can say about your issue is:
1) Look that your $PAGEID isn't correct. Actually it's your APPID instead.
2) After subscribing, have your subscription appeared on the list when calling https://graph.facebook.com/<app-id>/subscriptions?access_token=... ?
3) With user subscription, does it work? Or is it just with pages?
For this you need to set up an endpoint URL that receives both HTTP GET (for subscription verification) and POST (for actual change data) requests from Facebook.
Then you should make a POST to the graph API url https://graph.facebook.com/<app-id>/subscriptions?access_token=<access_token> to subscribe, and be ready to handle the verification request.
Then to list subscriptions, just perform a GET request on the same url, https://graph.facebook.com/<app-id>/subscriptions?access_token=<access_token>, which returns a JSON-encoded content that lists your subscriptions, up to one per object type.
Before you start waiting for the notifications you should check if the URL is actually being hit or not by FB
Check The Output of Following:
"https://graph.facebook.com/".FACEBOOK_APP_ID."/subscriptions?".$appToken."&client_secret=".FACEBOOK_SECRET;
This shall return the callback_url if set correctly !