can't exchange token manually v2.7 - facebook

I'm using Loopback and
I'm trying to user auth for graph api without javascript sdk or passport
I got the code successfully however I can't exchange it with access token
I followed this guide https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow/#confirm
my get request is https://graph.facebook.com/v2.7/dialog/oauth?code={xxxx}&client_secret={xxxx}&client_id={xxx}&redirect_uri={myURL}
myURL is the one used to get the code but not be used again if I understand
If I understand correctly I should it the access_token in the body of the response instead I get this error
{
"error": {
"message": "Unknown path components: /oauth",
"type": "OAuthException",
"code": 2500,
"fbtrace_id": "HXe+214tGpW"
}
}

It looks like a bug in the docs. The first call is to www.facebook.com in a browser.
See here for an example client https://github.com/yschimke/oksocial/blob/master/src/main/java/com/baulsupp/oksocial/services/facebook/FacebookAuthFlow.java
The second should be to something like https://graph.facebook.com/v2.7/oauth/access_token

$response = $fb->sendRequest(
'GET',
'/oauth/access_token',
[
'client_id' => $config['client_id'],
'client_secret' => $config['client_secret'],
'grant_type' => 'fb_exchange_token',
'fb_exchange_token' => $short_token
],
$short_token,
null,
'v2.7');

Related

How to create a Facebook ad post (promotable_posts) using koala gem?

I was trying to create a FB ads post through koala gem:
page_graph.put_connections(page_id,"promotable_posts",params)
where params
params = {
"message" => 'my_message',
"name" => "my_name",
"link" => "www.link.com",
"caption" => "my_caption",
"description" => "This is a longer description",
"picture" => "https://pic.com/1234"
}
The error am getting is
Koala::Facebook::ClientError: type: GraphMethodException, code: 100, error_subcode: 33, message: Unsupported post request. Object with ID '<page_id>' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api, x-fb-trace-id:XXXX [HTTP 400]
I have given all the permissions for creating ads posts(ads_management,ads_read) Also, I am able to get the ads_posts for a page:
page_graph.get_connections(page_id,"promotable_posts" ) but am not able to create one.
How can I debug this?

Creating Issue using GitHub API

I am trying to create an Issue in my own repo using Github API, but for some reason it keeps throwing me an error
Route::post('/issue/create/{repo}',function ($_repo){
$client = new Client([
// Base URI is used with relative requests
'base_uri' => 'https://api.github.com',
// You can set any number of default request options.
// 'timeout' => 2.0,
]);
$url = '/repos/rehan-dckap/'.$_repo.'/issues';
// Set various headers on a request
$response = $client->request('POST', $url, [
'query' => [
'title' => 'IssueCreation',
'body' => 'ThPI',
'assignee' => '',
'milestone' => 1,
'labels' => [],
'assignees' => []
],
'headers' => [
'Authorization' => 'Bearer TOKENTOKENTOKENTOKEN'
]
]);
return response($response->getBody());
});
ERROR
Client error: POST https://api.github.com/repos/rehan-dckap/qatouch-api-docs/issues?title=IssueCreation&body=ThPI&assignee=&milestone=1 resulted in a 422 Unprocessable Entity response: { "message": "Invalid request.\n\nFor 'links/0/schema', nil is not an object.", "documentation_url": "https://develo (truncated...)
Can someone guide me?
I've taken quite a bit of time to try and understand what the problem is here, but without running the code itself it will be a bit tricky. Can you provide an online sandbox with that code so I can play with it? I'm happy to provide the token myself. There's two main things here.
First the 422 generally means that there was an error parsing the payload. Looking at your payload, and the error message I would try to start by removing all non-mandatory fields starting by the arrays. If we look at the error message it's saying Nil is a not an object. My best guesses would be problems with the arrays or the assignee string.
Overall my tip in these cases is to reduce the API call to the bare functional. minimum and try to isolate the problem. I would go as far as using the GitHub Example they've posted on the API page and even remove the assignee since it's been deprecated:
{
"title": "Found a bug",
"body": "I'm having a problem with this.",
"milestone": 1,
"labels": [
"bug"
]
}
Hope this helps.
here you can check the error code: https://developer.github.com/v3/
Sending invalid fields will result in a 422 Unprocessable Entity response.
HTTP/1.1 422 Unprocessable Entity
Content-Length: 149
{
"message": "Validation Failed",
"errors": [
{
"resource": "Issue",
"field": "title",
"code": "missing_field"
}
]
}

Google Cloud Storage upload file to Facebook Attachment API

I have an app that I am trying to create a Facebook bot for and it needs to send photo cards to users. However, to do so I need to get a Facebook attachment_id because I can't upload my files directly from the web. (https://developers.facebook.com/docs/messenger-platform/reference/attachment-upload-api/)
My app is on Firebase, for some reason when I try the URL it gives me this error:
{
"error": {
"message": "(#546) The type of file you're trying to attach isn't allowed. Please try again with a different format.",
"type": "OAuthException",
"code": 546,
"error_subcode": 1545026,
"fbtrace_id": "H8qfi28cNvp"
}
}
That happens with any URL I try but here is a test https://memes-dev.mymemestore.com/mms-tp1pESD7hgWWLiOUziHz.jpg
So I try to use the Google Cloud Storage nodejs api to get a readstream and send that like so
const stream = gcs().bucket('my bucket').file('mms-tp1pESD7hgWWLiOUziHz.jpg').createReadStream();
const message = new formdata();
message.append('message', '{"attachment":{"type":"image", "payload":{"is_reusable":true}}}');
message.append('filedata', stream);
fetch('https://graph.facebook.com/v2.6/me/message_attachments?access_token=<PAGE ACCESS TOKEN>',
{
method: 'POST',
headers: message.getHeaders(),
body: message
})
.then(res => {
console.log(res);
return res.json();
})
.then(json => {
console.log(json);
});
but that gives me the following error
(#100) Incorrect number of files uploaded. Must upload exactly one file.
Does anyone know what is going on?
Thanks
One thing I forgot to mention was that this all had to be done serverless. But I got around it by downloading the image to a tmpdir using tmpdir = path.join(os.tmpdir(), filename) and then fs.createReadStream(tmpdir) and that is working.

Get page access token with Facebook API 5.0 PHP

I need to post messages on a Facebook page. Specifically I want to post via cron.
Here's what the API docs say:
Page Access Token – These access tokens are similar to user access tokens, except that they provide permission to APIs that read, write or modify the data belonging to a Facebook Page. To obtain a page access token you need to start by obtaining a user access token and asking for the manage_pages permission. Once you have the user access token you then get the page access token via the Graph API.
How I can obtain a user access and page access token without a page callback? Is this possible?
What you need it an Extended Page Token, it is valid forever. You get one like this:
Authorize with the manage_pages permission (and publish_pages if you want to post as Page later), to get a User Token
Extend the User Token
Use /me/accounts?fields=access_token with the Extended User Token to get a list of all your Pages with Extended Page Tokens - or use /page-id?fields=access_token to get an Extended Page Token for a specific Page
Information about all Tokens and how to extend the User Token:
https://developers.facebook.com/docs/facebook-login/access-tokens#extending
http://www.devils-heaven.com/facebook-access-tokens/
PHP API V5
The below code worked for me after 24 hours of head scratching .... hope this helps by the way if you need this code to work you should have completed the first two steps
Should have login to facebook I used getRedirectLoginHelper
Set session variable with the received user access token on the call back file $_SESSION['fb_access_token'] = (string) $accessToken;
$fbApp = new Facebook\FacebookApp( 'xxx', 'xxx', 'v2.7' );
$fb = new Facebook\Facebook( array(
'app_id' => 'xxx',
'app_secret' => 'xxx',
'default_graph_version' => 'v2.7'
) );
$requestxx = new FacebookRequest(
$fbApp,
$_SESSION['fb_access_token'],//my user access token
'GET',
'/{page-id}?fields=access_token',
array( 'ADMINISTER' )
);
$responset = $fb->getClient()->sendRequest( $requestxx );
$json = json_decode( $responset->getBody() );
$page_access = $json->access_token;
//posting to page
$requesty = new FacebookRequest(
$fbApp,
$page_access ,
'POST',
'/{page-id}/feed?message=Hello fans YYYYYYYYYYYYYYY'
);
$response = $fb->getClient()->sendRequest( $requesty );
var_dump( $response );
You can get the page token this way:
$response = $fb->get('/'.$pageId.'?fields=access_token', (string)$accessToken);
$json = json_decode($response->getBody());
$page_token = $json->access_token;
$response = $fb->post('/'.$pageId.'/feed', $fbData, $page_token);
I've only JavaScript code, but once you have an access token, you may get the pages which can be adminstered by the given user. This will contain a page access token for each of them:
jQuery.ajax({type: "GET",
url: "https://graph.facebook.com/v2.2/me/accounts?access_token=" + userToken,
async: false,
data: jsonRequest,
dataType: "json",
cache: false,
success: function(data)
{
The data given back is like:
{
"data": [
{
"access_token": "CAACni8TcBB0B...cZBJfwZDZD",
"category": "Computers/Technology",
"name": "abc",
"id": "...",
"perms": [
"ADMINISTER",
"EDIT_PROFILE",
"CREATE_CONTENT",
"MODERATE_CONTENT",
"CREATE_ADS",
"BASIC_ADMIN"
]
},
{
"access_token": "CAA...ZDZD",
"category": "App Page",
"name": "xyz",
"id": "....",
"perms": [
"ADMINISTER",
"EDIT_PROFILE",
"CREATE_CONTENT",
"MODERATE_CONTENT",
"CREATE_ADS",
"BASIC_ADMIN"
]
}
],
access_token is your page token. You may transform the above request into PHP easily.

Unable to publish custom story with mention tagging

My app was doing well yesterday and posting custom stories with mention tagging but from today its not working and telling me : -
Received Facebook error response of type OAuthException: Unknown path components: /mynamespace:customaction
Error code : 2500
{
"error": {
"message": "Unknown path components: /namespace:customaction",
"type": "OAuthException",
"code": 2500
}
}
$response = $facebook->api(
'me/mynamespace:customaction',
'POST',
array(
'treat' => "http://www.mywebsite.com/12321425125"
)
);
I have not changed any configurations or anything and it has stopped working all of a sudden. Please help me identify the issue - maybe there is an issue at Facebook end.