2

I am using unification engine #unificationengine API to post message on facebook. I followed all the steps and created connections to use connectors. All the curl requests are working fine till send message. In every curl from create user, create connection, connection refresh I am getting

{'status':200,'info':'ok'}

And now I want to use the connector to post message on facebook. Below is my Curl code:

$post_msg = json_encode(
        array(
            'message' =>
                array(
                    'receivers' =>
                        array(
                                array(
                                    'name'      => 'Me',
                                    'address'   =>'https://graph.facebook.com/'.$request->profile_id.'/feed?access_token='.$request->access_token.'&message=Hello&method=post',
                                    'Connector' => 'facebook'

                                ),
                        ),
                        'sender'    =>
                        array('address' => 'sender address'),
                        'subject'   => 'Hello',
                        'parts'     =>
                        array(
                                array(
                                    'id'          => '1',
                                    'contentType' => 'binary',
                                    'data'        => 'Hi welcome to UE',
                                    'size'        => 100,
                                    'type'        => 'body',
                                    'sort'        => 0

                            ),
                        ),
                    ),

                )
            );



    $ch = curl_init('https://apiv2.unificationengine.com/v2/message/send');
    curl_setopt($ch, CURLOPT_USERPWD, "0a7f4444-ae4445-45444-449-d9b7daa63984:8755b446-6726-444-b34545d-713643437560");
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_msg);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);




    // execute!
    $response = curl_exec($ch);

    // close the connection, release resources used
    curl_close($ch);

    // do anything you want with your response
    var_dump($response);



    return ['label' => $response];

and I am getting:

status: 403 and info: forbidden in response.

I have tried everything available in documentation and on stack overflow or any other website. But hard luck.

Please suggest why I am getting this error?

Refrence SO Questions:

  1. SO question 1

  2. SO question 2

Thanks.

Update I added these three options in curl request:

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, true); 

and now I am getting 498, invalid access token error:

"{\"Status\":{\"facebook\":{\"status\":498,\"info\":\"Invalid Token: \"}},\"URIs\":[] }

Community
  • 1
  • 1
Always_a_learner
  • 4,585
  • 13
  • 63
  • 112
  • 1
    Can you please check if it works by adding curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); If it still shows error, add curl_setopt($ch, CURLOPT_VERBOSE, true); and check what error it is showing. – AMT.in Jan 06 '17 at 10:14
  • Nothing changed in error, still getting \"facebook\":{\"status\":403,\"info\":\"Forbidden: \"}. I added all three options you mentioned. – Always_a_learner Jan 06 '17 at 10:20
  • @AMT.in I am getting another error now: {\"Status\":{\"facebook\":{\"status\":498,\"info\":\"Invalid Token: \"}},\"URIs\":[] – Always_a_learner Jan 06 '17 at 10:27
  • @AMT.in Can you guide me further if I am passing right json or if any information is missing. We are trying to figure out this from last 2 days. – Always_a_learner Jan 06 '17 at 10:31

2 Answers2

1

please use this as per php

public function facebookSharing($access_token) {
        $app = new UEApp(env('UNIFICATION_APP_KEY'), env('UNIFICATION_APP_SECRATE'));
        $user = new UEUser('unification_userkey', 'unification_usersecret');
        $connection = $user->add_connection('FACEBOOK', "facebook", $access_token);
        $options = array(
            "receivers" => array(
                array(
                    "name"=> "Me"
                )
            ),
            "message"=>array(
                "subject"=>'testing',
                "body"=> 'description',
                "image"=> 'use any image url',
                "link"=>array(
                    "uri"=> 'any web site url',
                    "description"=> "",
                    "title"=>"Title"
                )
            )
        );
        $uris = $connection->send_message($options);
    }
Kuldeep Raj
  • 791
  • 1
  • 7
  • 29
0

The access token might have expired. Please reconnect the facebook connection again or refresh the connection.

The facebook access tokens have a lifetime of about two hours. For longer lived web apps, especially server side, need to generate long lived tokens. Long lived tokens generally lasts about 60 days.

UE has a capability to refresh facebook tokens. After adding connection using "apiv2.unificationengine.com/v2/connection/add"; api call, then you should call "apiv2.unificationengine.com/v2/connection/refresh"; api to make the short lived token to long lived.

AMT.in
  • 391
  • 1
  • 5
  • Thanks. But UE refresh connection api is not helping either it is showing 200 Ok status but not returning uri in response as it suppose to be. Mentioned in documentation tht it will return uri along with success status. What to Do? Any further guidance? – Always_a_learner Jan 07 '17 at 22:12
  • It seems that the uri won't be returned in response, but UE just refreshes the token and returns the status. – AMT.in Jan 09 '17 at 05:28
  • I would like to tell you here that I am using API on my localhost. May be that is the reason that it is not working and giving invalid token error everytime. – Always_a_learner Jan 09 '17 at 06:06
  • For my one app connection refresh works but facebook send message give 403 error. For another app connection refresh and send message both are giving 498 invalid token error in response. – Always_a_learner Jan 09 '17 at 07:18