Error while posting app request in Facebook using app login - facebook

I am trying to post an apprequest to a user using the URL:
https://graph.facebook.com/USER_ID/apprequests?message=’This is a new message from the pgm’&data='t1t2t3t4’&access_token=ACCESS_TOKEN_RECEIVED_FROM_FB&method=post
I am getting the following error:
Response Message Bad Request Response Code 400 App Request ID: 400 Bad Request
Method Not Implemented
Invalid method in request
Note: I got the access token and the same url works fine in the browser (Chrome).
Am I missing something? Couldn't find much in the documentation!
Regards

You need to url-encode your parameters. The browser does this transparently to you, that's why it's working there. Assuming you're using php:
http_build_query(array(
"message" => "This is a new message from the pgm",
"data" => "t1t2t3t4",
"access_token" => ACCESS_TOKEN_RECEIVED_FROM_FB,
"method" => "post"
));
This will take care of the encoding and joining parameters via amperstand characters. The return value is:
message=This+is+a+new+message+from+the+pgm&data=t1t2t3t4&access_token=ACCESS_TOKEN_RECEIVED_FROM_FB&method=post

Related

HUAWEI Push - API for obtaining an access token not working

When I use this api to get access token:
https://oauth-login.cloud.huawei.com/oauth2/v3/token?grant_type=client_credentials&client_id=****&client_secret=****
it always returns
{
"sub_error": 20001,
"error_description": "missing required parameter: client_id",
"error": 1102 }
although I send client_id parameter
To solve the issue, Please make sure the below points
You are doing post request to the server
Make sure that the POST URL is “https://oauth-login.cloud.huawei.com/oauth2/v3/token”. Do not include post parameters in the URL
Add below parameters in body of POST request but not in the URL as mentioned here ( https://oauth-login.cloud.huawei.com/oauth2/v3/token?grant_type=client_credentials&client_id=****&client_secret=**** )
grant_type=client_credentials
client_id=****
client_secret=****
For more details about fetching the access token, please refer the below link:
https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides-V5/open-platform-oauth-0000001053629189-V5#EN-US_TOPIC_0000001053629189__section12493191334711
Please use the following scheme instead for oath authentication:
https://oauth-login.cloud.huawei.com/oauth2/v3/authorize?grant_type=client_credentials&client_id=\*\*\*\*&client_secret=\*\*\*
If you don't give this a valid client_id it will say that you have a "invalid client_id" instead of saying it's empty. Hope this helps. (The change is that the original says .../token? and this says .../authorize?)

How to login user from inside Facebook Page Tab and get access_token on server side to post a carousel to the feed?

I have a Page Tab facebook app. I want to post to users timeline from it.
After login on client side with javascript sdk (I use angularjs module for that Ciul/angular-facebook (sorry, cannot post github link here)):
https://gist.github.com/Sevavietl/7e363fdfd0e714a12a43
I retrieve access_token on server side and trying to post a carousel to the users feed:
https://gist.github.com/Sevavietl/cec5fa434837312adfd3
I have two problems:
While first call I get
Graph returned an error: (#210) Param id must be a page.
After browsing, I found that this can be caused by wrong token usage. Not user_access_token. But I login the user on the client side.
And for next calls I get
Graph returned an error: This authorization code has been used.
Again, after browsing, I found that token can be used only once in order to be OAuth compliant.
Please, advise me how to do this right?
Thank you in advance.
Best regards,
Vsevolod
After spending some time on this, I am writing my solution, as it was not really obvious, at least for me. But, actually, all info is present in the documentation.
Starting with problem number 2: And for next calls I get Graph returned an error: This authorization code has been used.
As it was mentioned in the comments, I indeed was confusing access_token with authorization code. As I understand in really simplified form, the mechanism is:
When you login with facebook-javascript-sdk it writes authorization code to cookies.
Then on the server side you can retrieve access_token from javaScriptHelper available in facebook-php-sdk. This helper has getAccessToken() method, which retrieves access_token using authorization code from cookies. This is the place where I was confused. I was trying to use getAccessToken() on every request. As requests were made with AJAX the authorization code was not changed, and as you can use it only once I was getting an error. The solution to this was pointed in many places on the Internet, but somehow I was able to misunderstand this.
Here is the code excerpt that uses sessions to store access_token, and uses getAccessToken() method only if access_token is not set in the session.
$fb = new Facebook([
'app_id' => $widget->app_id,
'app_secret' => $widget->app_secret,
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getJavaScriptHelper();
if ($request->session()->has('facebook_access_token')) {
$accessToken = $request->session()->get('facebook_access_token');
} else {
$accessToken = $helper->getAccessToken();
// OAuth 2.0 client handler
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
$request->session()->put('facebook_access_token', (string) $longLivedAccessToken);
$request->session()->save();
}
if ($accessToken) {
$fb->setDefaultAccessToken($accessToken);
} else {
die('No Access Token');
}
I use Laravel, so the session handling is not framework agnostic. This code is only for example, it is better to create service and move logic there.
Now. About the first problem: While first call I get Graph returned an error: (#210) Param id must be a page.
Again I was confusing two things '{user-id}/feed' and '{page-id}/feed'. My aim was to post a carousel with images and links. To create a carousel you have to provide child_attachments in the fields array. While you can send a Post like this to '{page-id}/feed', you cannot do so for '{user-id}/feed'. So at the moment you cannot post a carousel to users feed.
So when Graph Api was getting a Post data applicable for '{page-id}/feed', it was assuming that I have passed the {page-id}. And when getting the {user-id} instead, the Graph Api yelled back "(#210) Param id must be a page.".

Facebook JS SDK throwing 104 error when attempting to delete a app request

I have setup the facebook javascript sdk to initialize a friend invite dialog to send requests to friends. Clicking on the invite in Facebook, brings the user back to the site.
The query string has any and all request_ids that have not been handled (deleted).
I am following the documenation listed at the FB developers site here which states:
JavaScript SDK:
FB.api(requestId, 'delete', function(response) {
When they are returned to my site, the querystring has a request_ids parameter:
example: www.mysite.com/somepage?request_ids=0493834050
my Javascript code looks like this:
FB.api(requestIds[i], 'delete', function (response) {
console.log(response);
});
But the error I get is:
ERROR
code: 104
message: "An access token is required to request this resource."
type: "OAuthException"
All I want to do is delete the request ID. before I was even running the above code, the querystring was building up with every submitted request. It does appear they have been removed, because the querystring is reduced down to a single request id. But I am still uncertain it has been handled properly.
NOTE I am not using the Graph version - as I have not setup Graph on the app settings in Facebook.
I have the Facebook C# sdk - if someone has an easy way to do this here. I have tried the following with the same results.
var fbClient = new FacebookClient(accessToken)
fbClient.Delete(requestId);
I don’t see what’s possibly unclear about the error message – you need a valid access token to be able to delete requests, but by the looks of it you don’t have one.
So you’ll either have to have the user connect to your app in order to get an active access token for him; or you have to do it using your own app access token (but that’d have to be done server-side then, because you don’t want to transfer your app access token to the client side).

Requests 2.0, empty data in graph after successfully sending requests

I'm seeing some strange behaviour with Requests.
I initiate a requests dialog with:
method: 'apprequests'
to: 'someUid'
(and some data for message, title, data)
The user approves the Request, and I get back a valid response, giving me the request Id, the user Ids it was sent to, and the updatedFrictionless flag. However, when reading the request from the graph later with either:
https://graph.facebook.com/?ids=REQUESTID_USERID&access_token=RECEIVINGUSERTOKEN
or
https://graph.facebook.com/REQUESTID_USERID?access_token=RECEIVINGUSERTOKEN
I get back no data !
The first graph call will give me an empty array (an un-named array not 'data', just simply [ ] )
The second, gives me 'false'
REQUESTID is the request Id I get back from the original call, USERID is the receiving USERID.
The receiving user is me, and I do see the request in my notifications, but using the graph explorer to check my 'apprequests', its empty.
Any ideas? I have Requests 2.0 enabled, and call FB.init with frictionlessRequests: true..
Does it work with your app access token?

Facebook access_token invalid?

I'm attempting to use the new Graph API Facebook recently released, but I can't seem to get it to work correctly.
I've gone through the steps, and after the /authorize call, I receive an access_token:
access_token=109002049121898|nhKwSTJVPbUZ5JYyIH3opCBQMf8.
When I attempt to use that token I get:
{
"error": {
"type": "QueryParseException",
"message": "An active access token must be used to query information about the current user."
}
}
I'm stumped as too why...
-AC
When using your Facebook Application's token
If you're using the me alias as in https://graph.facebook.com/me/ but your token is acquired for a Facebook Application, then "me" isn't you anymore - it's the app or maybe nothing. Anyway, that's not your intention for the app to interact with itself.
In this case you will want to interact with your personal user account from an app. What you need to do (after giving the app the permissions it requests in the UI when it asks) is find your facebook userid # and put it in place of "me" to access your own info. e.g. Mark Zuckerberg's facebook userid is 4 so he is https://graph.facebook.com/4/
The alias me only works if you're you! Sometimes it's hard to remember who the current user is when programming facebook (i.e. you, the Page, the App, etc) because we're accustomed to using the facebook UI as ourselves most of the time. From a programming standpoint it depends on what the acquired token represents.
A great blog post that always helps correct me is Ben Biddington | Facebook Graph API — getting access tokens.
same thing here. I followed Ben Biddington's blog to get the access token. Same error when trying to use it. Facebook's OAuth implementation doesn't follow the spec completely, i am fine with it as long as the doc is clear, which obviously is not the case here. Aslo, it would be nice if the userid and username are returned with the access token.
Just to clarify -- after you call
https://graph.facebook.com/oauth/authorize?
you should receive a CODE which, in conjunction with your CLIENT_ID and CLIENT_SECRET (assuming you have registered your application) can be exchanged for an access_token at
https://graph.facebook.com/oauth/access_token?
If this is indeed how you came by your ACCESS_TOKEN, you should then be able to request
https://graph.facebook.com/me/
Adding type parameter returns the auth_token for the application level, so it is better to OMIT it. What worked for me, after countless attempts and combinations, is using the same redirect_url parameter in the call to /oath/access_token as was used in the call to /oath/authorize.
So the full sequence to authorize your app on someone's behalf is:
1. call or redirect to:
"https://graph.facebook.com/oauth/authorize?client_id=" + my_clientId + "&scope=publish_stream,offline_access,manage_pages" + "&redirect_uri=" + "http://my_redirect_url?blah"
2. in the page located at the return_url above, issue a request or what ever else to this url:
"https://graph.facebook.com/oauth/access_token?client_id=" + client_id + "&client_secret=" + secret + "&code=" + Request.QueryString["code"] + "&redirect_uri=" + "http://my_redirect_url?blah"
I've been having the exact same problem. A couple of things I've done to resolve it:
Try it all out in the browser first to make sure the urls are correct at each stage
Ensure the redirect url is identical, not just equivalent. Parameters in the same order, encoding the same
Don't use the type=client_cred, or anything else for that matter
Encode any ampersands in the redirect_url (but not the rest of the url) e.g. http://example.com/fb?foo=234%26bar=567. This one caused me the most issues. When the callback page was run, only the url before the first ampersand was included, as the ampersand was assumed to be part of the url for graph.facebook.com, not part of the redirect_url. I was then getting the values from the querystring to put in the redirect_url for the second call, but they weren't there. Once I encoded the ampersands they appeared correctly.
Don't have any empty values in you encoded querystring parameters (e.g. ?foo=%26bar=123)
I want to point out what has sort of been said on Ben Biddington's blog, and what I noticed from looking at the "malformed" access_token in the initial question. Others have said similar things in this thread, but I want to be explicit.
The token is not actually malformed, but rather a token that allows you to do actions on behalf of the APP, not the user. This is the token you'd use if you wanted to get all of the users of the app, or view insights for your app, etc, with the requests typically coming from your server, not the client. This type of token is gained by using the type=client_cred parameter. If you want to do things on behalf of the user, do not specify type=client_cred, and make sure you specify the following parameters in your call to http://graph.facebook.com/oauth/access_token:
'client_id' => APP_ID
'redirect_uri' => REDIRECT_URI
'client_secret' => APP_SECRET
'code' => $_GET['code']
I've written this as key-value pairs of a PHP array, but I think you get the point. The code GET value is gained after making the initial call to http://graph.facebook.com/oauth/authorize with the following parameters:
'client_id' => APP_ID
'redirect_uri' => "http://your.connect.url/some/endpoint"
I hope this helps! What the Facebook docs say, but don't say well, is that getting an access_token is a two-request process.
I actually noticed that if your return uri doesn't have a slash on the end you have issues. I'm currently testing in the browser and return_uri=https://mydomain.com doesn't work but return_uri=https://mydomain.com/ does work. If I use the first I get "Error validating verification code."
This seems a bit odd, but I prolly just missed a word in the spec/instructions some where. Did lose two hours of my life to it though.
I had the same problem, but getting rid of type=client_cred and making sure that the redirect_uri parameter is the same when making the authorize and the access_token call fixed the issue.
I had the same issue in IE8 only.
The solution for me was sending the access_token in the API request.
Something like this:
FB.api('/me/friends?access_token=<YOUR TOKEN>
I obtained my token through PHP like this:
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => '<API_ID>',
'secret' => '<SECRET>',
'cookie' => false,
));
$session = $facebook->getSession();
$token = $session['access_token'];