Comment Posted but OAuthException Returned - php

I'm using the official Facebook PHP SDK to interact with the graph API. According to the Facebook documentation here, I'm using the following API method to post a comment to a Facebook wall post:
public function post_comment($id, $comment) {
$publish = $this->facebook->api("/$id/comments", "post", array('message' => $comment));
return $publish;
}
The comment is successfully posted to Facebook: I can see it on Facebook's website after posting it, however the API itself returns:
PHP Fatal error: Uncaught OAuthException: An unexpected error has occurred. Please retry your request later.
Which in turn causes my server to return a 500 internal server error.
Given that the comment successfully posts, I don't see how there can be an OAuth exception?
Any help would be gratefully accepted.
EDIT:
I ran the same methods on Facebook's Graph API explorer, and got the same output. The comment posted fine, but the graph explorer threw the Uncaught OAuthException error, too. I have all the default permissions selected, including publish_stream and publish_actions. The specific output was:
{
"error": {
"message": "An unexpected error has occurred. Please retry your request later.",
"type": "OAuthException",
"code": 2
}
}

Hey here you go with some demo code.
The $POST_ID thingy must be set by you ofc.
try{
$response = $facebook->api("/".$POST_ID."/comments","POST",array (
'message' => 'This is a test comment',
)
);
}catch(FacebookApiException $e){
error_log($e->getMessage());
}

Related

LinkedIn API Returns "Member does not have permission to get companies as admin"

I am using PHP SDK for linkedIn https://github.com/zoonman/linkedin-api-php-client , the login process is fine and I am able to fetch basic profile for users. But when I fetch the list of pages that user is admin of , it throws fatal error.
Fatal error: Uncaught exception 'GuzzleHttp\Exception\ClientException' with message 'Client error: GET https://api.linkedin.com/v1/companies?is-company-admin=true resulted in a 403 Forbidden response: { "errorCode": 0, "message": "Member does not have permission to get companies as admin."
here is the code.
use LinkedIn\Client;
use LinkedIn\AccessToken;
$linkedClient = new Client('ID','SECRET');
$linkedInAccessToken = new AccessToken($_SESSION["linkedinToken"]);
$linkedClient->setAccessToken($linkedInAccessToken);
$companies = $linkedClient->get('companies',array('is-company-admin' =>
"true"));
var_dump($companies);
Two points to notify.
1) The linkedin user is actually an admin of two company pages.
2) Following permissions are given from linkedin App.
r_basicprofile, r_emailaddress, rw_company_admin, w_share
I have spent hours searching for solution but the documentation is too vague. Any help will be greatly appreciated. Thank you !
Update: I solved the issue by passing $scopes array in getLoginURL() function. Although all these scopes are still ticked mark in application settings.

failedPrecondition error when authenticating with service account to Google API

I'm trying to fetch a given email from my own inbox (as project owner) using the Google API 2.0.0 RC4 for PHP.
I have a project setup, a downloaded authentication file in JSON format and I have enabled the gmail API in the Google Developers Console.
But when I run the following code I get a "Bad Request" error with reason "failedPrecondition". Now this leads me to believe that there is something wrong with the access rights, but I can't figure out what I need to do here.
Code:
<?php
require_once("google-api-php-client-2.0.0-RC4/vendor/autoload.php");
$client = new Google_Client();
// set the scope(s) that will be used
$client->setScopes(["https://www.googleapis.com/auth/gmail.readonly"]);
$client->setApplicationName("MyProject");
// set the authorization configuration using the 2.0 style
$client->setAuthConfigFile("myAuthFile.json");
$gmailHandle = new Google_Service_Gmail($client);
$messages = $gmailHandle->users_messages->get("myemail#mydomain.com", "12343445asd56");
echo json_encode($messages);
?>
And the complete error looks like this:
Fatal error: Uncaught exception 'Google_Service_Exception' with message '{ "error": { "errors": [ { "domain": "global", "reason":
"failedPrecondition", "message": "Bad Request" } ], "code": 400,
"message": "Bad Request" } } '
I just ran into this and finally found the solution. You are getting that error because you are using a service account without specifying a "subject" as part of the configuration. That is required for the API to know which email address you are acting as when using the service account. You can see it attempting to use this subject in Google_Client::createApplicationDefaultCredentials
The solution is to add the following line in the code after you have called setAuthConfig()
$client->setConfig('subject', 'you#email.com');
Be sure to then use the keyword 'me' when fetching the messages from the inbox as you are now acting as the email address which you have configured as 'subject'.

oauth 401 unauthorized error for heroku app fitbit

I've got a heroku app and I am running with Auth0 as the way of logging in via oauth... I have some code more or less the same as from the fitbit api php tutorial - "completeAuthorization.php". It looks a bit like:
$oauth = new OAuth($conskey, $conssec, OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_AUTHORIZATION);
$oauth->enableDebug();
}
catch( OAuthException $E )
{
print_r($E);
}
echo 'done new oauth';
$oauth->setToken($_SESSION['access_token'], $_SESSION['access_token_secret']);
echo 'done set token';
// Fitbit API call (get activities for specified date)
//http://api.fitbit.com/1/user/28C9GG/activities/date/2013-12-01.xml
$apiCall2 = 'http://api.fitbit.com/1/user/'.$_SESSION['userid'].'/activities/date/2014-02-25.xml';
echo $apiCall2;
// Performing API call
$oauth->fetch($apiCall2);
//$oauth->fetch($apiCall);
var_dump($oauth->getLastResponse());
I get the user id, and the session secret and token etc. from the Auth0 response from my index.php page, but I save them in a session to keep them on my get activities page.
But when I go to my page that does this after I log in with Auth0, I get this error:
http://api.fitbit.com/1/user/28C9GG/activities/date/2014-02-25.xml Fatal error: Uncaught exception 'OAuthException' with message 'Invalid auth/bad request (got a 401, expected HTTP/1.1 20X or a redirect)'
I am also only able to get this far on my webserver which has a pecl oauth extension installed, but heroku does not and I have no idea how to get it on Heroku, any ideas on this or why I get the above error?
Here is an example that might be useful:
https://github.com/auth0/Auth0-PHP/tree/master/examples/fitbit
In particular look at callback.php where there is a call to fitbit API
Matias

LinkedIn API to post Share using php oauth class

The details:
I am using Manuel Lemos php oauth class and the login_with_linkedin.php script as reference.
I have successfully gone through the oauth process and retrieved the user's info using the API 'http://api.linkedin.com/v1/people/~'
I get an error when using the Post Shares API (http://api.linkedin.com/v1/people/~/shares)
Here is the relevent code from my last unsuccessful attempt;
// Scope settings
$client->scope = 'r_fullprofile r_emailaddress r_network rw_nus';
// Call to API (Post)
$success = $client->CallAPI(
'http://api.linkedin.com/v1/people/~/shares',
'POST', array(
"format"=>"json",
"comment"=> "Test"
), array('FailOnAccessError'=>true), $user);
Here is the error message:
Error: it was not possible to access the API call: it was returned an unexpected response status 401 Response: { "errorCode": 0, "message": "Unknown authentication scheme", "requestId": "8DWA0EBJTB", "status": 401, "timestamp": 1355774186502 }
Any help would be greatly appreciated. Thank you.
I posted the same issue on the class creator's forum as well.
He got back to me with an answer that worked.
The answer can be found here:
http://www.phpclasses.org/discuss/package/7700/thread/22/

Twitter OAuth Fetch 404 Error

I am working with Twitter OAuth in PHP framework and having an issue with getting through Twitter OAuth process. Everything seems to go through just fine and tokens match up all the way up to the last step that fetches user information.
The code is as follows:
...
$oauth->setToken($oauth_token, $session->twitter_secret);
$access_token_info = $oauth->getAccessToken(conf::get('TWITTER_OAUTH_ACCESS_URL'));
$session->twitter_state = 2;
$session->twitter_token = $access_token_info['oauth_token'];
$session->twitter_secret = $access_token_info['oauth_token_secret'];
$session->twitter_id = $access_token_info['user_id'];
$oauth->setToken($session->twitter_token, $session->twitter_secret);
if ($oauth->fetch(conf::get('TWITTER_API_URL').'/account/verify_credentials.json'))
{
...
}
Here is the exception error output:
500 exception 'OAuthException' with message 'Invalid auth/bad request (got a 404, expected HTTP/1.1 20X or a redirect)' in /var/www/webapp/app/controllers/default/auth.controller.php:135
Stack trace:
#0 /var/www/webapp/app/controllers/default/auth.controller.php(135): OAuth->fetch('https://api.twi...')
Now I am at a loss as to why there is a 404 error being thrown. It also does not make sense that the error happens after all authentication steps have been passed.
Thanks for any help in advance.
Looks like the solution is described over here:
Getting error with oauth
Seems that Twitter API needs to have a version number now.

Categories