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?

Answers


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

Need Your Help

making a box2d body move in a spiral motion

cocos2d-iphone box2d physics game-physics box2d-iphone

How can one make a box2d body move in a spiral motion? I believe it will require applying some force but I am not sure how. I would appreciate any assistance.

Scraper implemented with python, using scrapy and selenium launches but shuts down

python selenium xpath web-scraping scrapy

I am having difficulty to achieve my scraper (I took the initial example code from here[selenium with scrapy for dynamic page from @alecxe, and completed for getting some results, but if the scraper