I have managed to post to Facebook Page via API (C#), but when administrator of the page logs out, the following error occurs:
\"(OAuthException - #190) Error valida
Using Facebook API v3.1 - None of the answers above worked for me. Instead, I had to:
1) Create a "system user"
2) Grant him access to the properties I needed (in my case an App)
3) Generate a new token for that app and system user
The instructions I used can be found here
podrias intentar algo como esto
Administrar Paginas
<a href="#" class="btn" onclick="token_live()" >url</a>
<script type="text/javascript">
function token_live(){
var token_app = "";
$.ajax({
url: "https://graph.facebook.com/v2.8/oauth/access_token?grant_type=fb_exchange_token&client_id=598062314053459&client_secret='client_secret'&fb_exchange_token=access_token",
type: 'POST',
dataType: 'HTML',
data: {api_public: 'AP-42b3a8aab70',
},
})
.done(function(data) {
var txt = data
var obj = JSON.parse(txt);
var token_live = obj.access_token
var url_infinit = "https://graph.facebook.com/v2.8/oauth/access_token?grant_type=fb_exchange_token&client_id='remplaza_cliente_id'&client_secret='client_secret'&fb_exchange_token="+token_live;
alert(url_infinit);
```
This is the code that I use to generate "Never" expire access token using PHP SDK:
$facebook = new \Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.10',
'default_access_token' => '{access-token}'
]);
// Exchange token
$token = $facebook->post('/oauth/access_token',
array(
'grant_type' => 'fb_exchange_token',
'client_id' => 'APP ID',
'client_secret' => 'APP Secret',
'fb_exchange_token' => 'access Token'
)
);
$token = $token->getDecodedBody();
$token = $token['access_token'];
echo $token;
I echo the access token and then debug it using the access token debugger. The result should be: Expires: Never
.
References from the Documentation:
You can generate never expiring access token without coding, following this instructions:
Select your app created above and select “Get user access token in the drop down”
In the user access token pop up you can select some permissions for the token. For an non expiry page access token you need to select "publish pages" and "manage pages"
Go to https://developers.facebook.com/tools/accesstoken/. There you will find short lived user access tokens and app access token of all the apps you have
Press debug option of user access token of the app created above. This will take you to the debug tool. Where you can find all the information of short lived user access token.
In the bottom there is option to generate long lived(60 days) user access token for this short lived user access token. Generate long lived user access token by selecting “Extend Access Token”
a. Go to the Graph Explorer - https://developers.facebook.com/tools/explorer/.
b. Paste the long lived user access token generated in previous step inside “Access token” field.
c. Access “/me?fields=access_token” api . This will result page access tokens and pages related to them. These page access tokens will never expire(until user change the password/user revoke the app)
a. Go to https://developers.facebook.com/tools/debug/accesstoken/
b. Add the page access token retrieved from above step into “Access token “ field and debug
You will get expires as Never
Found here with little changes: https://medium.com/@Jenananthan/how-to-create-non-expiry-facebook-page-token-6505c642d0b1
this Makefile works as of 2015-10-29. steps 2 and 3 give only a two-month token, but the page access token given in the final step shows in the debugger as "Expires: Never". this answer draws upon the work of several others, and is provided in the hopes that it will simplify things for developers regardless of preferred programming language.
before using this, you need to put your existing page ID, app ID, and app secret, in that order, in your ~/.netrc file as follows: machine graph.facebook.com login 123456 account 234567 password 345678
also before using this, login to Facebook with w3m, clicking "Keep me logged in".
MACHINE := graph.facebook.com
PAGE_ID := $(shell awk '$$2 ~ /^$(MACHINE)$$/ {print $$4}' $(HOME)/.netrc)
APP_ID := $(shell awk '$$2 ~ /^$(MACHINE)$$/ {print $$6}' $(HOME)/.netrc)
APP_SECRET := $(shell awk '$$2 ~ /^$(MACHINE)$$/ {print $$8}' $(HOME)/.netrc)
PERMISSIONS := manage_pages,publish_actions,publish_pages
FB := https://www.facebook.com
GRAPH := https://$(MACHINE)
CODE ?=
TOKEN ?=
TWOMONTHTOKEN ?=
BROWSER ?= w3m -dump
REDIRECT := http://jc.unternet.net/test.cgi
CLIENT_SIDE := $(FB)/dialog/oauth?client_id=$(APP_ID)&redirect_uri=$(REDIRECT)
CLIENT_SIDE := $(CLIENT_SIDE)&scope=$(PERMISSIONS)&response_type=code
SERVER_SIDE := $(GRAPH)/oauth/access_token?client_id=$(APP_ID)
SERVER_SIDE := $(SERVER_SIDE)&redirect_uri=$(REDIRECT)
SERVER_SIDE := $(SERVER_SIDE)&client_secret=$(APP_SECRET)&code=$(CODE)
LONG_LIVED := $(GRAPH)/oauth/access_token?client_id=$(APP_ID)
LONG_LIVED := $(LONG_LIVED)&client_secret=$(APP_SECRET)
LONG_LIVED := $(LONG_LIVED)&grant_type=fb_exchange_token
LONG_LIVED := $(LONG_LIVED)&fb_exchange_token=$(TOKEN)
ACCOUNTS := $(GRAPH)/me/accounts?access_token=$(TWOMONTHTOKEN)
export
env:
env
@echo Usage: make code
@echo ' ' make CODE=codefrompreviousstep token
@echo ' ' make TOKEN=tokenfrompreviousstep longterm
@echo ' ' make TWOMONTHTOKEN=tokenfrompreviousstep accounts
@echo Then edit '$$HOME/.netrc' replacing password with page token
code:
$(BROWSER) "$(CLIENT_SIDE)"
token:
$(BROWSER) "$(SERVER_SIDE)"
longterm:
$(BROWSER) "$(LONG_LIVED)"
accounts:
$(BROWSER) $(ACCOUNTS)
it turns out in many cases the first step fails with w3m. in that case, install another browser such as firefox; ssh -X
to your server if the script is remotely hosted; and use make BROWSER=firefox code
instead. the following steps should work with w3m as shown.
note: if cutting-and-pasting this Makefile, make sure to replace the 4-space indentations with proper tabs.