2010-05-06 15 views
12

Ho creato un'app e ora voglio inviare un messaggio su uno dei miei amici wall con l'utilizzo della nuova Graph API. È fattibile?Come utilizzare FB Graph per inviare un messaggio su un feed (muro)

Sto già utilizzando oAuth e Graph-api per ottenere un elenco di tutti i miei amici. L'API a http://developers.facebook.com/docs/api mi dice di CURL https://graph.facebook.com/[userid]/feed a leggere il feed, ma dice anche me howto postare un messaggio:

curl -F 'access_token=[...]' -F 'message=Hello, Arjun. I like this new API.' https://graph.facebook.com/arjun/feed 

Naturalmente questo non funziona! E non riesco a scoprire perché ..

Ecco il mio PHP-code:

require_once 'facebook.php'; // PHP-SDK downloaded from http://github.com/facebook/php-sdk 
$facebook = new Facebook(array(appId=>123, secret=>'secret')); 
$result = $facebook->api(
     '/me/feed/', 
     array('access_token' => $this->access_token, 'message' => 'Playing around with FB Graph..') 
); 

Questo codice non genera alcun errore, e so che il mio access_token siano corretti (altrimenti i could't corro $ facebook-> api ('/ me access_token =?' $ this-> access_token.);.? per ottenere la mia UserObject

avere qualcuno là fuori documento con successo ha postato un messaggio utilizzando Graph-api Poi ho bisogno del vostro aiuto ! :-)

risposta

18

Va bene, ho finalmente risolto questo. Grazie a phpfour per il vostro aiuto :-)

Primo: La mia connessione-url si presenta così (con "publish_stream"):

$connectUrl = $this->getUrl(
    'www', 
    'login.php', 
    array_merge(array(
    'api_key'   => $this->getAppId(), 
    'cancel_url'  => $this->getCurrentUrl(), 
    'req_perms'  => 'publish_stream', 
    'display'   => 'page', 
    'fbconnect'  => 1, 
    'next'   => $this->getCurrentUrl(), 
    'return_session' => 1, 
    'session_version' => 3, 
    'v'    => '1.0', 
), $params) 
); 

Seconda; Ho provato a postare i tuoi amici tramite

$result = $facebook->api(
    '/me/feed/', 
    array('access_token' => $this->access_token, 'message' => 'Playing around with FB Graph..') 
); 

Ma il modo corretto per farlo è includere un altro parametro ('post'):

$result = $facebook->api(
    '/me/feed/', 
    'post', 
    array('access_token' => $this->access_token, 'message' => 'Playing around with FB Graph..') 
); 
6

Avrai bisogno del "publish_stream" esteso permesso per scrivere sul feed. Ecco un elenco completo di loro: http://developers.facebook.com/docs/authentication/permissions.

Al fine di ottenere il permesso esteso, ottenere il token di autorizzazione in questo modo:

https://graph.facebook.com/oauth/authorize? 
client_id=...& 
redirect_uri=http://www.example.com/callback& 
scope=publish_stream 
+0

Oggi sto usando Facebook :: getLoginUrl() di autorizzare. Ho aggiunto "publish_stream" sotto "req_perms", ma immagino che non sia abbastanza buono :( – qualbeen

+0

Beh, se sei bloccato puoi dare un'occhiata al mio recente post sul blog in cui ho mostrato un esempio operativo: http: //bit.ly/cT40vV –

+1

Vale la pena notare che dovrebbe essere "publish_stream", non "stream_publish". Vedi http://developers.facebook.com/docs/authentication/ –

0

Questo è il vecchio modo di ottenere accesso. Nel grafico prima ho generato codice chiave con:

$getLinkCode ='https://graph.facebook.com/oauth/authorize'. 
       '?client_id=YOUR_APPID'. 
       '&redirect_uri=YOUR_SITE'. 
       '&scope=publish_stream'; 

E ora, quando abbiamo codice tasto possiamo generare access_token dal link:

$getLinkToken='https://graph.facebook.com/oauth/access_token'. 
       '?client_id=YOUR_APPID'. 
       '&redirect_uri=YOUR_SITE'. 
       '&client_secret=YOUR_SECRET'. 
       '&code=CODE_KEY'; 

Ma questo access_token inviare il messaggio come UTENTE non APPLICAZIONE ... PERCHÉ ?!

Se si vuole scrivere un commento sul uso della parete di applicazione:

$facebook->api('/YOUR_APPID/feed/', 'post', array('access_token' => $this->access_token, 'message' => 'Playing around with FB Graph..')); 
1

Oltre a chf,

Dopo aver postato:

$getLinkToken='https://graph.facebook.com/oauth/access_token'. 
       '?client_id=YOUR_APPID'. 
       '&redirect_uri=YOUR_SITE'. 
       '&client_secret=YOUR_SECRET'. 
       '&code=CODE_KEY'; 

ho ricevuto la risposta:

https://graph.facebook.com/oauth/access_token? 
    client_id=xxxxxxxxxxxxxx 
    &redirect_uri=myurl 
    &client_secret=xxxxxxxxxxxxxx 
    &code=xxxxxxxxxxxxxx 

senza che uno è access_token, client_secret o il codice

$facebook->api('/YOUR_APPID/feed/', 'post', 
array('access_token' => $this->access_token, 
'message' => 'Playing around with FB Graph..')); 
1

Come il link dice: enter link description here

<?php 
    $app_id = "YOUR_APP_ID"; 
    $app_secret = "YOUR_APP_SECRET"; 
    $my_url = "YOUR_POST_LOGIN_URL"; 
    $code = $_REQUEST["code"]; 
    if(empty($code)) { 
    $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" 
    . $app_id . "&amp;redirect_uri=" . urlencode($my_url) . "&amp;scope=email"; 
    echo("<script>top.location.href='" . $dialog_url . "'</script>"); 
    } 
    $token_url = "https://graph.facebook.com/oauth/access_token?client_id=" 
    . $app_id . "&amp;redirect_uri=" . urlencode($my_url) 
    . "&amp;client_secret=" . $app_secret 
    . "&amp;code=" . $code; 
    $access_token = file_get_contents($token_url); 
    $graph_url="https://graph.facebook.com/me/permissions?".$access_token; 
    echo "graph_url=" . $graph_url . "<br />"; 
    $user_permissions = json_decode(file_get_contents($graph_url)); 
    print_r($user_permissions); 
?> 
0

Invece di utilizzare il codice qui sotto

[facebook dialog:@"feed" 
andParams:params 
andDelegate:self]; 

Usare la seguente soluzione

[facebook requestWithGraphPath:@"me/feed" andParams:params andHttpMethod:@"POST" andDelegate:self]; 
Problemi correlati