0

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?

Community
  • 1
  • 1

3 Answers3

0

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/

Lix
  • 47,311
  • 12
  • 103
  • 131
  • I understand what you mean but in my application i want my users login only one time. Why we can post links or statuses on behalf of the user with application access token and we cannot post photos with this kind of token ? – Tony Eurogiciel Jul 17 '12 at 09:54
  • @tony, you are mistaken - the app access token can not create status updates on behalf of a user. With an app access token you can posts updates as page perhaps - but not on behalf of a "normal" user. – Lix Jul 17 '12 at 09:56
  • You're right i meant statuses updates with "via My Application". Why is there not the same functioning with photos? – Tony Eurogiciel Jul 17 '12 at 10:07
  • when you retrieve a token, you do so using your app id... any token you have is connected to the application therefore any action you make will be associated to your app... – Lix Jul 17 '12 at 10:12
  • That's ok but if my user can only log in one time, i store in my DB his user access token. Few days later i want to publish photos in his profile but his user access token has expired, may i retrieve a new token without a new user login ?? – Tony Eurogiciel Jul 17 '12 at 10:23
  • Yes you can - read this post for info on extending the validity of an access token - http://facebook.stackoverflow.com/questions/8982025/how-to-extend-access-token-validity-since-offline-access-deprecation – Lix Jul 17 '12 at 10:28
  • Thanks for your replies ! Don't you think that facebook should allow applications to post photos with application access token someday ? – Tony Eurogiciel Jul 17 '12 at 10:44
  • You can do it - although the photo's will be posted on behalf of the application and not the user... – Lix Jul 17 '12 at 10:46
  • do you mean post as application on application wall or user wall ?? – Tony Eurogiciel Jul 20 '12 at 08:10
0

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'];
}
CBroe
  • 91,630
  • 14
  • 92
  • 150
Needhi Agrawal
  • 1,326
  • 8
  • 14
  • Dude, please format your postings a little more careful – not everything is “code”. – CBroe Jul 18 '12 at 09:05
  • but in my app, users log in only one time in order to authorize application to publish on their walls, after the login users are disconnected from facebook (using FB.logout()). Users will never log in again – Tony Eurogiciel Jul 19 '12 at 12:56
  • _“Users will never log in again”_ – that will not work anymore; offline_access is deprecated; and this is _intentional_ behaviour. You are not supposed to be able to build an app (any more) that can go on doing stuff in the user’s name until forever. Long-lived access tokens can be valid for 60 days – after that, your user’s will _have_ to interact with your app again. – CBroe Jul 25 '12 at 12:55
0
$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);
love
  • 132
  • 1
  • 2