{"id":1011,"date":"2022-08-30T15:10:54","date_gmt":"2022-08-30T15:10:54","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/facebook-60-day-access-token-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:10:54","modified_gmt":"2022-08-30T15:10:54","slug":"facebook-60-day-access-token-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/facebook-60-day-access-token-collection-of-common-programming-errors\/","title":{"rendered":"Facebook 60 day access token-Collection of common programming errors"},"content":{"rendered":"<p>I am currently having issues with the 60 day access_token. I grab the fansite access_token thru \/me\/applications and I want to post on them, without the need for refreshing the site every 2 hours.<\/p>\n<p>When I&#8217;m parsing the token for the fansite in the way facebook provides it just throws an error.<\/p>\n<p>The login is handled thru Javascript and in the background with the PHP SDK.<\/p>\n<p>Greetings, Moritz<\/p>\n<p>Edit:<\/p>\n<p><code>{\"error_code\":1,\"error_msg\":\"An unknown error occurred\"}<\/code> is the error.<\/p>\n<p>Code is this:<\/p>\n<pre><code>$attachment = array('message' =&gt; $post['title'],\n                        'link' =&gt; $unique,\n                        'actions' =&gt; '{\"name\": \"Fanseiten Admin?\", \"link\": \"http:\/\/google.com\"}',\n                        'access_token' =&gt; $page['access_token']);\n$result = $facebook-&gt;api('\/'.$page['pageid'].'\/feed', 'post', $attachment);\n<\/code><\/pre>\n<p>Simply pushes to a site with the access token stored in a database.<\/p>\n<ol>\n<li>\n<p>I found the solution: extend the base_facebook.php class with following and call<\/p>\n<pre><code>$facebook-&gt;setAccessToken($facebook-&gt;getExtendedAccessToken());\n<\/code><\/pre>\n<p>The access_token for apps also now extends to 60 days.<\/p>\n<pre><code>public function getExtendedAccessToken(){\n\n    try {\n        \/\/ need to circumvent json_decode by calling _oauthRequest\n        \/\/ directly, since response isn't JSON format.\n        $access_token_response =\n            $this-&gt;_oauthRequest(\n                $this-&gt;getUrl('graph', '\/oauth\/access_token'), array(\n                    'client_id' =&gt; $this-&gt;getAppId(),\n                    'client_secret' =&gt; $this-&gt;getAppSecret(),\n                    'grant_type'=&gt;'fb_exchange_token',\n                    'fb_exchange_token'=&gt;$this-&gt;getAccessToken()\n                )\n            );\n    } catch (FacebookApiException $e) {\n        \/\/ most likely that user very recently revoked authorization.\n        \/\/ In any event, we don't have an access token, so say so.\n        return false;\n    }\n\n    if (empty($access_token_response)) {\n        return false;\n    }\n\n    $response_params = array();\n    parse_str($access_token_response, $response_params);\n    if (!isset($response_params['access_token'])) {\n        return false;\n    }\n\n    return $response_params['access_token'];\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:13:25. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am currently having issues with the 60 day access_token. I grab the fansite access_token thru \/me\/applications and I want to post on them, without the need for refreshing the site every 2 hours. When I&#8217;m parsing the token for the fansite in the way facebook provides it just throws an error. The login is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1011","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1011","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1011"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1011\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}