cant get email with Net::Facebook::Oauth2 - perl

my $fb = Net::Facebook::Oauth2->new(
application_id => 'your_application_id',
application_secret => 'your_application_secret',
callback => 'http://yourdomain.com/facebook/callback'
);
my $access_token = $fb->get_access_token(code => $cgi->param('code'));
###save this token in database or session
##later on your application you can use this verifier code to comunicate
##with facebook on behalf of this user
my $fb = Net::Facebook::Oauth2->new(
access_token => $access_token
);
my $info = $fb->get(
'https://graph.facebook.com/me' ##Facebook API URL
);
print $info->as_json;
when i try to print the json format of response i am missing the email the followin is the output what i get
{"id":"100001199655561","name":"Pavan Kumar Tummalapalli","first_name":"Pavan","middle_name":"Kumar","last_name":"Tummalapalli","link":"http:\/\/www.facebook.com\/pavan.tummalapalli","username":"pavan.tummalapalli","hometown":{"id":"125303864178019","name":"Kodada, India"},"location":{"id":"115200305158163","name":"Hyderabad, Andhra Pradesh"},"favorite_athletes":[{"id":"108661895824433","name":"AB de Villiers"}],"education":[{"school":{"id":"129163957118653","name":"City Central School"},"type":"High School"},{"school":{"id":"124833707555779","name":"Anurag Engineering College"},"year":{"id":"136328419721520","name":"2009"},"type":"College"}],"gender":"male","timezone":5.5,"locale":"en_US","verified":true,"updated_time":"2012-12-30T09:13:54+0000"}
'https://graph.facebook.com/me?fields=email
the i get the following reponse as
{"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}}

Probably you do not have adequate permission to access that data. The code you provided doesn't indicate if you have requested email permission on authorization process so I'm guessing you didn't request it.
When you redirect your user to Auth Dialog, which is like https://www.facebook.com/dialog/oauth/?client_id=YOUR_APP_ID&redirect_uri=YOUR_REDIRECT_URL&state=YOUR_STATE_VALUE&scope=COMMA_SEPARATED_LIST_OF_PERMISSION_NAMES , you have to specify scope=email to acquire permission to access email field.
To check if you have the permission, you can access the URL below and see if you have it.
my $permission_ref = $fb->get(
'https://graph.facebook.com/me/permissions'
);
The returning value should be something like below.
{
"data": [
{
"installed": 1,
"email": 1
}
]
}
If it includes email, you have the permission to access user email.
If not you'll have to request permission to get it. With Net::Facebook::OAuth2, you can generate this dialog url like below.
my $fb = Net::Facebook::Oauth2->new(
application_id => 'your_application_id',
application_secret => 'your_application_secret',
callback => 'http://yourdomain.com/facebook/callback'
);
my $url = $fb->get_authorization_url(
scope => ['email'],
);
Redirect your user to this url and you'll get the permission.
And even if you have email permission, sometimes email is not returned because of a bug. You might want to check this bug report, "API call to /me is missing user's email even with email permission."

Related

Error pulling companies user is admin of in LinkedIn

I'm currently trying to pull the list of company pages a user is the admin of with the LinkedIn API and am getting the following response:
Array
(
[response] => {
"errorCode": 0,
"message": "Member does not have permission to get companies as admin.",
"requestId": "R1LHP32UKD",
"status": 403,
"timestamp": 1482357250945
}
[http_code] => 403
)
The call works perfectly when authenticated as the same user in the LinkedIn API Console.
Has anyone else come across this?
I figured out the problem.
Even though I had made sure the app had the rw_company_admin permission checked off, I wasn't passing that in the scope when requesting oath2 authorization.
My fixed code below:
The call:
return $linkedin->getAuthorizationUrl("r_basicprofile w_share rw_company_admin", $thisurl);
The function:
public function getAuthorizationUrl($scope, $callback)
{
$params = array('response_type' => 'code',
'client_id' => $this->api_key,
'scope' => $scope,
'state' => uniqid('', true), // unique long string
'redirect_uri' => $callback,
);
// Authentication request
$url = 'https://www.linkedin.com/uas/oauth2/authorization?' . http_build_query($params);
// Needed to identify request when it returns to us
$_SESSION['Linkedin_state'] = $params['state'];
$_SESSION['Linkedin_callback'] = $callback;
// this is where to send the user
return $url;
}
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.

get the facebook reviews return error

I first get page token with facebook api
function createSession($app_id,$app_secret){
FacebookSession::setDefaultApplication(
$app_id,$app_secret
);
$testUserPermissions = array('user_actions.books','user_actions.fitness','user_actions.music','user_actions.news','user_actions.video','user_birthday','user_games_activity','user_groups','user_hometown','user_interests','user_location','user_relationship_details','user_religion_politics','user_tagged_places','user_videos','user_work_history','read_friendlists','read_page_mailboxes','manage_notifications','read_mailbox','publish_actions','read_stream', 'user_photos','manage_pages','public_profile','user_friends','email','user_about_me','user_activities','user_education_history','user_events','user_likes','user_relationships','user_status','user_website','read_insights','rsvp_event');
$testUserPath = '/' . $app_id . '/accounts/test-users';
#$testUserPath = '/100008488695640/accounts/test-users';
$params = array(
'installed' => true,
'name' => 'User test',
'locale' => 'zh_TW',
'permissions' => implode(',', $testUserPermissions),
);
echo var_dump($params);
$request = new FacebookRequest(new FacebookSession($app_id . '|' .$app_secret), 'POST', $testUserPath, $params);
$response = $request->execute()->getGraphObject();
echo var_dump($response);
#$testUserId = $response->getProperty('id');
$testUserAccessToken = $response->getProperty('access_token');
return new FacebookSession($testUserAccessToken);
}
this is my get token
then i use this token to get reviews
https://graph.facebook.com/v2.2/185342243407/ratings?field=open_graph_story&access_token=$token_value
but it return
{
"error": {
"message": "(#210) This call requires a Page access token.",
"type": "OAuthException",
"code": 210
}
}
how i can get the page token?
the token is not used?
Make sure you are REALLY using a Page Token. Put the Token in the Debugger and see if it´s a Page Token: https://developers.facebook.com/tools/debug/
More information about Tokens:
https://developers.facebook.com/docs/facebook-login/access-tokens/
http://www.devils-heaven.com/facebook-access-tokens/
When you request multiple permissions, some of them may work with the endpoint you are calling, and some of them may not, causing this 210 error.
In my case, adding profile_pic permission to /me?fields=id,first_name,last_name,email caused this error.

Perl wrapper around Facebook OAuth v2.0 protocol

I try to get some details from user of app, but have problem because don't know what to do with code I get after I run this code below, I get something like this:
http://myapp.com/?code=AQAPYR33UZrJpuPrAfE_lL3XPCBaw1ZcTLRl_9cYU2H77T7S3VKVz0njBt_0Jff54HfrzHW9_r-WF_-GN1VWHjEi9zNIMHZHFSEqXcTD7vbP2HKqN1DaEAealNxBNpATFb-RAFYIAZL4EqSUXLCYJ9-WvvDF5oWCUd5vAQ3TiXgPUDWml35v43fBn2xXJo1
I thought its access token but it didn't...
use CGI;
my $cgi = CGI->new;
use Net::Facebook::Oauth2;
my $fb = Net::Facebook::Oauth2->new(
application_id => 'your_application_id',
application_secret => 'your_application_secret',
callback => 'myapp.com'
);
###get authorization URL for your application
my $url = $fb->get_authorization_url(
scope => ['offline_access','publish_stream'],
display => 'page'
);
####now redirect to this url
print $cgi->redirect($url);
##once user authorizes your application facebook will send him/her back to your application
##to the callback link provided above
###in your callback block capture verifier code and get access_token
my $fb = Net::Facebook::Oauth2->new(
application_id => 'your_application_id',
application_secret => 'your_application_secret',
callback => 'http://myapp.com'
);
my $access_token = $fb->get_access_token(code => $cgi->param('code'));
###save this token in database or session
##later on your application you can use this verifier code to comunicate
##with facebook on behalf of this user
my $fb = Net::Facebook::Oauth2->new(
access_token => $access_token
);
my $info = $fb->get(
'https://graph.facebook.com/me' ##Facebook API URL
);
print $info->as_json;

How to publish photos on behalf of the user with application access token ? (not with user access token)

I want my application to publish photos, user has already registered my app with publish_stream permission and is currently disconnected from facebook.
Documentation https://developers.facebook.com/docs/publishing/ said :
To publish a 'photo' object you need
a valid access token
publish_stream permission
I tried a HTTP POST request :
https://graph.facebook.com/<USER_ID>/photos?access_token=<APPLICATION_ACCESS_TOKEN>
POST param : "url":"<link_to_some_picture>"
And i got an exception :
content={"error":{"message":"A user access token is required to request this resource.","type":"OAuthException","code":102}}
To publish photos on behalf of user i cannot pass a user access token... Why i can post links but not photos with my application access token?
Your title states that you are using an application access token - the error you are getting clearly states the problem. You need a user access token.
You'll need to extend your users access token in order to perform actions on his/her behalf when they are not connected to Facebook.
Here is a good resource for information about dealing with expired tokens and how to refresh them -
https://developers.facebook.com/docs/authentication/access-token-expiration/
When user registered your application then you can store the user's user access token after you can used that access token to post on behalf of user.When user come to your application next time you can update that access token.
You can use following function to get extended access token:
public function getExtendedAccessToken(){
try {
// need to circumvent json_decode by calling _oauthRequest
// directly, since response isn't JSON format.
$access_token_response =
$this->_oauthRequest(
$this->getUrl('graph', '/oauth/access_token'),
$params = array( 'client_id' => $this->getAppId(),
'client_secret' => $this->getApiSecret(),
'grant_type'=>'fb_exchange_token',
'fb_exchange_token'=>$this->getAccessToken(),
));
} catch (FacebookApiException $e) {
// most likely that user very recently revoked authorization.
// In any event, we don't have an access token, so say so.
return false;
}
if (empty($access_token_response)) {
return false;
}
$response_params = array();
parse_str($access_token_response, $response_params);
if (!isset($response_params['access_token'])) {
return false;
}
return $response_params['access_token'];
}
$fid = $row[1];
echo "fid = $fid\n";
$message =$row[2];
echo "msg = $message\n";
$friends = $facebook->api("/$user/friends?fields=id");
$args= array(
'app_id' => $facebook->getAppId(),
'to' => $fid,
'link' => 'http://www.facebook.com',
'message'=>$message,
);
$post_id = $facebook->api('/fid /feed', 'POST', $args);
var_dump($post_id);

Posting an image to fan page album using Facebook API

I have created an Facebook App. I need user to be able to post images to my facebook page's album through that App and for that I need to grab Access Token of the page. I can only get access token when I am logged in as an administrator of the page but if I log in with other account I don't get the access token of the page (Even though I liked the page and installed the App). Code is below
$fanpage = 'XXXXXXXXXXXXX';
$pages_arr = array(
'access_token'=>$access_token,
'fields'=>'access_token'
);
$page_token = $facebook->api('/'.$fanpage, 'get', $pages_arr);
$fanpage_token = $page_token['access_token'];
**//When logged in as admin of the page I get the token but I don't get the token if I am not //page admin**
echo $fanpage_token;
$file = "#".realpath("test.jpg");
$args = array(
'message' => 'test',
'image' => $file,
'aid' => $album_id,
'no_story' => 1,
'access_token' => $fanpage_token // note, we use the page token here
);
$photo = $facebook->api("/$album_id/photos", 'post', $args);
if( is_array( $photo ) && ! empty( $photo['id'] ) )
echo 'Photo uploaded. Check it on Graph API Explorer. ID: ' . $photo['id'];
I asking for following permissions on App install
manage_notifications,user_photos,photo_upload,publish_stream,friends_photos,manage_pages
I think this is intended behaviour. The manage_pages permission gives access to pages which the user administrates. If you need other (non admin) users to upload to your page, you'll need to store a valid access token for the page in your app. Then, use the saved access token to upload the image provided by the user.