file_get_contents not working with following url (failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request)
$token_url = "https://graph.facebook.com/oauth/access_token?client_id=235326466577139&redirect_uri=http%3A%2F%2Fapps.facebook.com%2Flikeablephotos%2F&client_secret=CLIENT_SECRET&code=AQDFZbjpAUda8c_gz4wDDuBOVrsn0dApz3s8UA--7hFQIi1wb70-tDE56xXcCtDq-hV5UWzR5YEw_ozuGT24FLfvh9KnqHZ3xdn46P_KxYCf3DHJQA3AAu2ICHBqTk1-6fHTsl6FbagKz83H6dn15kkbKksajA4KcVIoPse5JbuBLlh6V5L1ANe8fzR94iH_SMU";
$response = file_get_contents($token_url);
but if you copy and paste the above URL into the browser address bar it works just fine! and returns:
access_token=AAADWBzZAyUvMBAL2Th6CRtxh2Up5soTCK8N4HJcy0ZBhQgJPxtZArKbuITISMoGLDxNiyeNW4GUZCBvJPeBTH6mx4v83ueUIAAYQJA1WrAZDZD&expires=5112501
but his similar URL (for a different user) also works:
$token_url = "https://graph.facebook.com/oauth/access_token?client_id=235326466577139&redirect_uri=http%3A%2F%2Fapps.facebook.com%2Flikeablephotos%2F&client_secret=CLIENT_SECRET&code=AQC2kTEV96-1Cki2oYUhyzjH6yFe6AJRd1Q3G8fbUXW-IsLJUlactzSwCvGVBK6jh1tL-t7v6dOWJZzbkSYhk0n2z6BHQcpljWAdoXFGB5zLC4FgW8fmxT6hwdRIQOr2dZ95CD_q5yJuOUz_2DItUa3_FF9m2_TmFYGEbxPoiaF47YSTUuZp6g-8ffziJcKDAdo";
when using file_get_contents
Please help, thanks
As an alternative to file_get_contents have you considered using cURL. I use curl for alot of requests with great results and on fail it will not expose your client secret.
refer to http://php.net/manual/en/book.curl.php
this code snippet is a standard for all my apps to get application access token. Can be used for all api calls.
$app_access_token = GetCH();
function GetCH(){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&grant_type=client_credentials");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
if(substr($url,0,8)=='https://'){
// The following ensures SSL always works. A little detail:
// SSL does two things at once:
// 1. it encrypts communication
// 2. it ensures the target party is who it claims to be.
// In short, if the following code is allowed, CURL won't check if the
// certificate is known and valid, however, it still encrypts communication.
curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_ANY);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
}
$sendCH = curl_exec($ch);
curl_close($ch);
return $sendCH;
};
Finally found the answer, had the same issue..the problem is most likely because facebook's code that they give you has spaces in between the params. If you just copied and pasted from Facebook, you will get a HTTP/1.1 400 Bad Request error. Put all the params on one line and remove all spaces and this should fix it. Worked for me. I know this is an older post but if you are having this problem let me know if it works for you!
For debugging:
Take the actual string in $token_url and paste into a browser address field and see what happens.
You will get some back some json error code.
For me, it was the my token was expired. Starting fresh request worked fine.
same problem happened to me the problem was access token supplied with the URL was expired since access token have 1 hour validity ,so i created a new access token and it works again ,hope this info helps .
Related
What is the restful API url to remove a token from a topic? I know that the POST api call to add a token to a topic is like the following:
https://iid.googleapis.com/iid/v1/{token}/rel/topics/{topicName}
Documentation is less than helpful when I want a simple list of restful endpoints that I can use from my server. Thanks.
Given that this is a REST API, so I'd expect a DELETE request to the same URL should work.
But I admit that the documentation is not very clear on this, so please report back if it works (or doesn't).
Google support got back to me. POST'ing to a batchRemove did the trick, per documentation.
https://iid.googleapis.com/iid/v1:batchRemove
Just confirming, that in order to unsubscribe from a topic you have to use
method DELETE:
fetch('https://iid.googleapis.com/iid/v1/'+tokenz+'/rel/topics/movies', {
method: 'DELETE',
headers: new Headers({
'Authorization': 'key=****'
})
If you are using PHP and curl instead of
curl_setopt( $ch, CURLOPT_POST, true ); you must use curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
I am just trying to authenticate from NetSuite to docusign. I have my demo account, and am able to send requests successfully using the legacy authentication from postman.
However, when I try the same code in a server side situation, i get the INVALID_TOKEN_FORMAT error.
I've only built 3 or 4 apis so would really appreciate what I might be missing that's causing this 401 error.
NetSuite code (js syntax):
headers = {
'Accept':'application/json',
'Content-Type':'application/json',
'X-DocuSign-Authentication':{'Username':'abcusername','Password':'password123','IntegratorKey':'xyz123'}
};
res = nlapiRequestURL('https://demo.docusign.net/restapi/v2/login_information', '', headers, 'GET');
Ok solved! The authentication object needed to be in xml. Confused bc of the content type and the documentation shows JSON
We are sending about 1 lac email daily using sendgrid API.
But what happen is thousand of mail are undelievered to the recipient.
we want to get the list of these undelievered mail. How is that possible ?
Please suggest some idea or links. becouse I am lost and would really appreciate the assistance.
Thank you.
wow..! finally i made it...
curl code-
curl -X GET
https://sendgrid.com/api/bounces.get.json?api_user=undefined&api_key=XXXXXXXX
and here is my the php code
$ch =curl_init('https://sendgrid.com/api/bounces.get.jsonapi_user=10timeslocal&api_key=10timeslocal');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
Thanks a lot to everyone tried to solve this.
I'd start with reading the API docs for bounces, blocks, invalid emails, and spam reports.
You can also consume SendGrid's Event Webhook which will send you post requests with the information you are looking for.
have a table in the database, that must contains the log of all emails send to users. then later fetch status of these emails via sendgrid apis. then update their status as success, bounce, invalid.
then for next time don't send emails to that persons whose status in your list are bounce or invalid.
this way you can avoid sending emails to bounces or invalid users.
I've noticed that some facebook pages redirect. For example the NOFX band page (http://www.facebook.com/pages/NOFX/104336479603261 only two links allowed so that one isn't set to link) redirects to their official page ( https://www.facebook.com/pages/NOFX-Official-Page/180985116576?rf=104336479603261 ). I'm curious is if in the api we can tell that a page does this. https://graph.facebook.com/104336479603261 doesn't seem to show anything about the redirect but, perhaps there's another way to find it.
Edit:
Solutions that don't use the api are fine.
Edit2:
Solved here is the code I used if anyone is interested:
Code mostly copied from How can I determine if a URL redirects in PHP?.
function getURL($URL)
{
$ch = curl_init($URL);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3');
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
return $code;
}
Only thing really worth noting is that I had to add user agent so I don't get sent to an unsupported browser page.
Edit: Solutions that don't use the api are fine.
Then just make an HTTP HEAD request for the page URL, and see if you get a 301 Moved Permanently status code with a Location header in response. (Actually checking the status code only should be enough, if you only want to find out if the page gets redirected; if you also want to know where it’s getting redirected to, then check the location header as well.)
I am using java and the purpose of my demo application is simple: Update user status.
I followed the Server-side Flow on page http://developers.facebook.com/docs/authentication. I got the auth dialog, facebook lead to the callback url and I got the code in my callback page. Then I failed when I try to generate an access token.
In the guide page, it says the following url could be used to generated an access token:
https://graph.facebook.com/oauth/access_token?
client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&
client_secret=YOUR_APP_SECRET&code=THE_CODE_FROM_ABOVE
But what happens in my environment is I got the following error message:
{
"error": {
"type": "OAuthException",
"message": "Error validating verification code."
}
}
I am quite sure every parameter is correct because if I change the client_id value or client_secret parameter, I will got a different error message. The code parameter is what I got from facebook callback. So this should be correct, right? Really can't figure out what is the problem....
Any idea about this? I get stuck here...
I recently dealt with exactly this problem: everything matched, but it failed with the OAuthException. The thing that made it work was to change the redirect uri (in both requests for the flow) from:
http://foo.example.com
to
http://foo.example.com/
I.e., add the trailing slash. And then it worked. Stupid and silly, but there you go.
I had the same problem and tried the above suggestions. They helped, but in my case the problem was that my redir URL had a query parameter and Facebook wasn't cool with that. So, moral of the story is that the redir url you sent to exchange the token has to be identical the the original redir url and it can't have query parameters.
We had some fun with this as well.
In our case the trailing slash in the URL was already there, so I tried the Token we were using in the FB Debug Tool and it validated, so it looked like FB wasn't even seeing the Token in the request.
After some investigation I found the head-slapper - we doing a GET with HTTP Headers only not with a Querystring, so FB litterally wasn't seeing the Token at all.
The moral seems to be that if you can get the Token to validated in the FB Debug tool, there is likely /something/ amiss in your request -
It might be a missing "/" or some other mismatch with the App's defined URL (Domain mistmatch is a different error). I have not tried defining the App / Web Url for HTTPS and doing the request with HTTP but I suspect it would also hiccup somehow.
Or as in our case, the Request Method might be incorrect - GET with Headers or POSTing both throw the 2500, you have to do GET with a Querystring.
Hope that helps!
I had the same problem. It was a URL difference, but unlike the others that have posted, mine was the difference between HTTP and HTTPS.
We have BigIP handling HTTPS requests and forwarding over to an HTTP Apache server. When BaseFacebook's getCurrentUrl() function was called, it detected HTTP, and not the original HTTPS. I've modified that function like so:
protected function getCurrentUrl() {
if ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1)) ||
(isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') ||
(isset($_SERVER['HTTP_PSEUDOSSL']) && $_SERVER['HTTP_PSEUDOSSL'] == 'true')) {
$protocol = 'https://';
}
else {
$protocol = 'http://';
}
...
This version supports the HTTP_PSEUDOSSL key. I hope this helps someone.
Yes, the trailing slash worked for me too, thanks!
For debugging purposes, I found it helpful to use exactly the code fb provides on the developer page:
http://developers.facebook.com/docs/authentication/
Once you get that working, you can modify it to fit your own code.
I'm not sure, but you might also check to make sure your "Site URL" and "Site Domain" settings are correct on the App Edit screen, because according to the documentation, the redirect_uri must be in the same domain. (This is different from the canvas/tab page urls.)
I was also having a url problem, but the solution to it is different. I was passing the signedRequest that the JavaScript SDK returns to the server, and using the code value from that to request an access-token. However, according to some comments in the 3.1.1 version of the Facebook PHP SDK, the JavaScript SDK associates the code with a redirect_uri of empty string, i.e. "":
// the JS SDK puts a code in with the redirect_uri of ''
if (array_key_exists('code', $signed_request)) {
$code = $signed_request['code'];
$access_token = $this->getAccessTokenFromCode($code, '');
if ($access_token) {
// etc
}
}
After I changed my own server-side code to use a redirect_uri of "", then the request for an access-token worked.
in my case, my code wasn't working on IE.
The issue was in the following line
$user_id = $facebook->getUser();
if ($user_id)
Because somehow the getUser function always returned 0, so that condition was always true. Than he generated that error from invalid token.
Well, i fixed it by just saying this:
if ($user_id>0)
Silly stuff...