403 forbidden error while sending messages to face

2019-07-07 15:29发布

问题:

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\":[] }

回答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);
    }


回答2:

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.