Note that there are some explanatory texts on larger screens.

plurals
  1. POHowTo: send app request, and get data from request (to redirec to to a specific link) without user accepting the app?
    text
    copied!<p>let's say user send app requests to his friends, this request will be targeting something /my_app/catalog.php?ID=25</p> <p>(not the index page) the only way to store specific link in app request is to use "data" field of the request.</p> <p>when user follow app request it will end up on index.php (not catalog.php?ID=25)</p> <p>how could i extract data field from request without knowing user id? (and without him accepting the app ?)</p> <p>i can get request_ids and also app_token (not user_access_tocken) by using <a href="https://graph.facebook.com/oauth/access_token?client_id=" rel="nofollow">https://graph.facebook.com/oauth/access_token?client_id=</a>".$GLOBALS['app_id']."&amp;client_secret=".$GLOBALS['app_secret']."&amp;grant_type=client_credentials</p> <p>but without the user id and him accepting the app it is impossible to get "data" field so instead of user been able to see a product in my app that his friend recommended him , he sees auth forms and accept the app dialogs whiout knoing what is this app about.</p> <p>this is not a right behavor and to my undestanding first user had to see a link on my page and later on if he want to do actions or my app need permissions for user's info - only when should the "accept dialog" be used.</p> <p>UPDATE: I guess Juicy right and where is no other way to get app request url but to either store it in your own database or require user to accept the app</p> <p>in case if someone else looking for this workaround here is some usefull stuff:</p> <pre><code>//to deal with app requests: (this is part of overall page output preparation) $rqlink=""; if((strlen($_REQUEST['request_ids'])&gt;0)&amp;&amp;(strlen($user-&gt;id)&gt;0))//user is logged no need for database { if(($rq=getfbres("https://graph.facebook.com/?ids=".$_REQUEST['request_ids']."&amp;access_token=".$_SESSION['access_token'] ))!==false) { $rqj = json_decode($rq); $request_ids = explode(',', $_REQUEST['request_ids']); foreach ($request_ids as $request_id) { $full_request_id=$request_id.'_'.$user-&gt;id; if(isset($rqj-&gt;$request_id-&gt;data)) $rqlink=$rqj-&gt;$request_id-&gt;data; //if(getfbres("https://graph.facebook.com/$full_request_id?method=delete&amp;access_token=".$_SESSION['access_token'])===false) //{ echo "delete request error:". $GLOBALS['err']; exit;} break; } } } elseif(strlen($_REQUEST['request_ids'])&gt;0) //user is not logged, try to extract url from database { $request_ids = explode(',', urldecode($_REQUEST['request_ids'])); foreach ($request_ids as $request_id) { if(!isset($conn)){include_once "conn.php"; $conn=init_conn();} if(!($rez=mysql_query("select * from ff_app_rq where rq='".str_replace("'","''",$request_id)."'",$conn))) die ("Database error"); if(mysql_num_rows($rez)&gt;0) { $row=mysql_fetch_assoc($rez); $rqlink=$row['url']; mysql_free_result($rez); break; } else //request not found for some reason and user is not authorized { //force pop-up authorization in order not to loose req_ids mysql_free_result($rez); echo("&lt;script LANGUAGE='javascript'&gt;window.open('/fblogin.php','_blank','width=900,height=450');&lt;/script&gt;"); break; } } } if(strlen($rqlink)&gt;0) { session_write_close(); echo("&lt;script LANGUAGE='javascript'&gt;document.location.href='".$rqlink."';&lt;/script&gt;"); // echo("&lt;script LANGUAGE='javascript'&gt;top.location.href='http".(getenv("HTTPS") == 'on'?"s":"")."://apps.facebook.com/".$GLOBALS['app_namespace']."/".$rqlink."';&lt;/script&gt;"); exit; } </code></pre> <p>this is how you promote:</p> <pre><code> function fire_promo() { FB.init({appId:'&lt;?php echo $GLOBALS['app_id'];?&gt;',status:true,cookie:true,frictionlessRequests:true,oauth:true}); //(this will display popup with list of fb friends) getpage('ajforms.php?ID=&lt;?php echo $id;?&gt;&amp;stage=promote_app','subcontent,-100,10,560,500,2,zzz'); return false; } function sendRequestToManyRecipients() { var f=document.send_inv; //(this form lists all checked users) var ids=""; for(var z=0;z&lt;f.length;z++) if(f[z].name=='p') if(f[z].checked) {if(ids.length&gt;0) ids+=',';ids+=f[z].value;} FB.ui({method:'apprequests',data:'catalog.php?ID=&lt;?php echo $id;?&gt;',message:'You have been invited to bla-bla',to:"'"+ids+"'"}, requestCallback); } </code></pre> <p>here is app request javascript callback:</p> <pre><code> function requestCallback(response) { if(response === undefined) return; //if(response.request===undefined) return; var req = getconn(); if(req) {req.open("HEAD",'fb_req.php?rq='+response.request+'&amp;url='+encodeURIComponent('catalog.php?ID=&lt;?php echo $id;?&gt;',true));req.send(null);} //console.log(response.request); if(response.to.length === undefined) {} else if(response.to.length&gt;0) alert('You have successfully promoted to '+response.to.length+' friends.\nThank You!'); } </code></pre> <p>(getconn is a standart ajax function to initialize ajax communications)</p> <p>here is fb_req.php to log app requests:</p> <pre><code>&lt;?php if((strlen($_REQUEST['rq'])&gt;0)&amp;&amp;(strlen($_REQUEST['url'])&gt;0)) { include_once "conn.php"; $conn=init_conn(); $rez=mysql_query("insert into my_app_rq_table (rq,url,rq_date) VALUES('".str_replace("'","''",$_REQUEST['rq'])."','".str_replace("'","''",urldecode($_REQUEST['url']))."',now())",$conn); //if(!$rez) die ("Database error".mysql_error()); } exit; ?&gt; </code></pre> <p>and finnaly here is fblogin.php for pop-up authorization and when refreshing opener window:</p> <pre><code>&lt;?php session_start(); include_once "params.php"; $code = $_REQUEST["code"]; if(strlen($code)&gt;2) { $my_url = "http".(getenv("HTTPS")=='on'?"s":"")."://".getdom().((($_SERVER['SERVER_PORT']=="80")||($_SERVER['SERVER_PORT']=="443"))?(""):(":".$_SERVER['SERVER_PORT']))."/fblogin.php?fb_redirect_url=".urlencode($_REQUEST['fb_redirect_url']); $token_url = "https://graph.facebook.com/oauth/access_token?client_id=".$GLOBALS['app_id']."&amp;redirect_uri=".urlencode($my_url)."&amp;client_secret=".$GLOBALS['app_secret']."&amp;code=".$code; $access_token = getfbres($token_url); $graph_url = "https://graph.facebook.com/me?".$access_token; $rr=strpos($access_token,"&amp;"); if($rr&gt;0) $access_token=substr($access_token,0,$rr); $access_token=str_replace("access_token=","",$access_token); $user = json_decode(getfbres($graph_url)); if(strlen($user-&gt;id)&gt;0) { $_SESSION['access_token']=$access_token; if(strlen($_REQUEST['fb_redirect_url'])&gt;0) echo "&lt;SCRIPT LANGUAGE='javascript'&gt;\nwindow.opener.location.href='".urldecode($_REQUEST['fb_redirect_url'])."';\nwindow.close();&lt;/script&gt;"; else echo "&lt;SCRIPT LANGUAGE='javascript'&gt;\nwindow.opener.location.reload(true);\nwindow.close();&lt;/script&gt;"; exit; } } if(strlen($_SESSION['access_token'])&gt;2) { $graph_url = "https://graph.facebook.com/me?access_token=".$_SESSION['access_token']; $user = json_decode(getfbres($graph_url)); if(strlen($user-&gt;id)&gt;0) { if(strlen($_REQUEST['fb_redirect_url'])&gt;0) echo "&lt;SCRIPT LANGUAGE='javascript'&gt;\nwindow.opener.location.href='".urldecode($_REQUEST['fb_redirect_url'])."';\nwindow.close();&lt;/script&gt;"; else echo "&lt;SCRIPT LANGUAGE='javascript'&gt;\nwindow.opener.location.reload(true);\nwindow.close();&lt;/script&gt;"; exit; } } $my_url = "http".(getenv("HTTPS")=='on'?"s":"")."://".getdom().((($_SERVER['SERVER_PORT']=="80")||($_SERVER['SERVER_PORT']=="443"))?(""):(":".$_SERVER['SERVER_PORT']))."/fblogin.php?fb_redirect_url=".urlencode($_REQUEST['fb_redirect_url']); $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=".$GLOBALS['app_id']."&amp;redirect_uri=".urlencode($my_url).$GLOBALS['app_scope']; //echo "&lt;h1&gt;".$_SESSION['log_attempts']."&lt;/h1&gt;"; $_SESSION['log_attempts']=intval("0".$_SESSION['log_attempts'])+1; if(intval("0".$_SESSION['log_attempts'])&lt;5) echo("&lt;script&gt;document.location.href='".$dialog_url."';&lt;/script&gt;"); else echo "&lt;center&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;h1&gt;Facebook Login failed&lt;/h1&gt;&lt;br&gt;Please refresh web page and try again.&lt;/center&gt;"; exit; function getdom() { return strtolower(str_replace("www.","",$_SERVER['SERVER_NAME'])); } function getfbres($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); $output = curl_exec($ch); $info = curl_getinfo($ch); if(curl_error($ch)) $err=curl_error($ch); else $err=""; curl_close($ch); //echo "&lt;pre&gt;info:&lt;br&gt;"; var_dump($info); echo "&lt;br&gt;output:&lt;br&gt;"; echo $output; if (strlen($err)&gt;0) echo "&lt;br&gt;error:&lt;br&gt;$err"; echo "&lt;/pre&gt;&lt;br&gt;"; if ($output === false || $info['http_code'] != 200) { // $GLOBALS['err']=$output."\nhttp code:".$info['http_code']."\n".$err; // echo "&lt;pre&gt;info:&lt;br&gt;"; var_dump($info); echo "&lt;br&gt;output:&lt;br&gt;"; echo $output; if (strlen($err)&gt;0) echo "&lt;br&gt;error:&lt;br&gt;$err"; echo "&lt;/pre&gt;&lt;br&gt;"; // exit; return false; } return $output; } ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload