We have code that was working perfectly to access the Googleplus Api, but now it inexplicably no longer works, instead returning a "(401) Invalid Credentials" error when this line is called:
$me = $plus->people->get('me');
Here is the full code snippet (php):
try
{
$client = new apiClient();
$client->setApplicationName("Google OAuth");
$client->setClientId(GOOGLE_API_CLIENT_ID);
$client->setClientSecret(GOOGLE_API_CLIENT_SECRET);
$client->setRedirectUri(GOOGLE_API_REDIRECT_URI);
$client->setDeveloperKey('XXX');
$client->setScopes(array('https://www.googleapis.com/auth/plus.me'));
$plus = new apiPlusService($client);
$oauth2 = new apiOauth2Service($client);
$client->authenticate();
$token = $client->getAccessToken();
if (isset($token))
$client->setAccessToken($token);
if ($client->getAccessToken()) {
$me = $plus->people->get('me');
}
}
catch (Exception $x)
{
echo "Stack: " . $x->getTraceAsString() . "<br />";
echo "Message: " . $x->getMessage() . "<br />";
echo "File: " . $x->getFile() . "<br />";
echo "Line: " . $x->getLine(). "<br />";
}
I had the same error when user denied access to my aplication, and the token remained in the DB (or session). The API was trying to authenticate using old token. You need to check
if(strpos($x->getMessage(), 'Invalid Credentials')) {
unset($_SESSION['access_token'];
}
and also remove any relevant data from your db.
What fails? the first call to authorize, or the 2nd one to get the token ?
Check the URLs that are sent, it will be easier to inspect the issue.
and: Google might have changed their oAuth APIs?
Everything checks out as far as I can tell, it's possible that something went wrong with the credentials associated with your account.
To verify this, try using another account to access the API or try revoking your application from Google accounts. To revoke your application from your account:
Navigate to this page: https://www.google.com/settings/account
Click the Security link and then click the Manage access button on the bottom of the page.
Find the application that corresponds to your site and click
After your site/application is revoked, go back to your site and then re-authenticate.
This problem is caused when you login from other device using same credentials and then logout .
Related
I am using this code to get the user friend's list. For profile picture i am unable to find any reference please guide if you know the process
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$iLimit=99;
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$user_profile = $facebook->api('/me','GET');
echo"<h3>";
echo 'Name: ' . $user_profile['name'];
echo"</h3>";
$profile_pic = "http://graph.facebook.com/".$user_id."/picture";
echo "<img src=\"" . $profile_pic . "\" />";
$user_friends = $facebook->api('/me/friends', 'GET' );
print_r($user_friends); //Given NULL Value
echo "</pre>";\
Returning null value
Please refer to the facebook graph api docs
https://developers.facebook.com/docs/graph-api/reference/friend-list/
and here is the code with tutorial
http://www.sanwebe.com/2011/10/facebook-friends-list-pagewise-with-php
Hope it helps !
I am using following code in soap api for forgot password for magento and how can send mail for change password link perticular user throw api using magento functionality mail using if any simple mathed for related rest api guide me
error_reporting(0);
$client = new SoapClient('http://127.0.0.1/mycart/api/soap/?wsdl');
$session = $client->login('admin', '123456789');
$return_array = array ();
$result = $client->call($session, 'customer.list');
$email=addslashes($_POST['email']);
//$email=addslashes("test#mail.com");
if(!empty($email)){
foreach ($result as $key=>$value){
//echo $value[email];
if ($value[email]==$email)
{
$return_array['message']="You will receive an email with a link to reset your password ";
$return_array['status']= "1";
echo json_encode($return_array);
exit;
}elseif ($value[email]!=$email) {
$return_array['message']="You are not registered with us. Please sign up to create an account";
$return_array['status']= "2";
echo json_encode($return_array);
exit;
}
}
}
else {
$return_array['message']="You are not registered with us. Please sign up to create an account ";
$return_array['status']= "0";
echo json_encode($return_array);
exit;
}
exit;
I guess you are using admin credentials for client login also you are using magento api V1 whether Magento v1 or v2 api needs api user. Please follow the guidance
https://www.yireo.com/tutorials/magebridge/administration/596-step-by-step-create-a-magento-api-user
Anyone know the proper way to authenticate and publish directly to a calendar without relying on a currently logged in user? Several weeks ago I built a calendar that used the standard Oauth 2.0 protocol but this relied sessions stored by a user's we browser. I have one calendar that I want to pass events to from an application I am writing with a basic PHP framework. I'm more concerned with what are the best practices that others are using. Your answer could be simply, don't do it. Thanks alot.
Use OAuth 2 and the Authorization Code flow (web server flow), with offline enabled. Store the refresh tokens (which last indefinitely until the user has revoked), and you'll be able to upload events to Google Calendar even when the user isn't currently logged in.
More info:
https://developers.google.com/accounts/docs/OAuth2WebServer#offline
try Zend_Gdata_Calendar with this library you are able to insert or get events from any user(with the right username and password obviously) from google calendar and integrate with your own calendar or display it..here a short example:
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient('gmail#user.com', 'gmailpassword', $service);
$service = new Zend_Gdata_Calendar($client);
$query = $service->newEventQuery();
$query->setUser('default');
$query->setVisibility('private');
try {
$eventFeed = $service->getCalendarEventFeed($query);
} catch (Zend_Gdata_App_Exception $e) {
echo "Error: " . $e->getMessage();
}
echo "<ul>";
foreach ($eventFeed as $event) {
echo "<li>" . $event->title . " (Event ID: " . $event->id . ")</li>";
}
echo "</ul>";
$eventURL = "http://www.google.com/calendar/feeds/default/private/full/Dir0FthEpUbl1cGma1lCalendAr";
try {
$event = $service->getCalendarEventEntry($eventURL);
echo 'Evento: ' . $event->getTitle() .'<br>';
echo 'detalles: ' . $event->getContent().'<br>';
foreach ($event->getWhen() as $dato)
{
echo 'inicia: ' . substr($dato->startTime, 0,-19) . ' a las: ' . substr($dato->startTime, 11,-10) .'<br>';
echo 'termina: ' .substr($dato->endTime,0,-19) . ' a las: ' . substr($dato->endTime,11,-10) .'<br>';
}
} catch (Zend_Gdata_App_Exception $e) {
echo "Error: " . $e->getMessage();
}
with this you can add, update, edit or delete events from calendar form any user with mail and password...
I'm creating a module for joomla that lists the the contents of a given folder. This module works as a service, so no OAuth should be required.
I'm stuck since every request I make has the same answer, no items. So I would like to know if I'm creating the client/service in a wrong way or I may have registered the application in a wrong way.
Here is the code:
// this class is where I have all the IDs from the google console
require_once(dirname(__FILE__) . '/gdrive/config.php');
require_once(dirname(__FILE__) . '/gdrive/gd-v2-php/apiClient.php');
require_once(dirname(__FILE__) . '/gdrive/gd-v2-php/contrib/apiDriveService.php');
$client = new apiClient();
$key = file_get_contents(Config::KEY_FILE);
$client->setClientId(Config::CLIENT_ID);
$client->setUseObjects(true);
$client->setAssertionCredentials(new apiAssertionCredentials(
Config::SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/drive'), $key)
);
$service = new apiDriveService($client);
try {
$parameters = array();
$parameters['maxResults'] = $maxResults;
$files = $service->files->listFiles($parameters);
$items = $files->getItems();
var_dump($items);
$pageToken = $files->getNextPageToken();
} catch (apiServiceException $e) {
print "Error code :" . $e->getCode() . "\n";
// Error message is formatted as "Error calling <REQUEST METHOD> <REQUEST URL>: (<CODE>) <MESSAGE OR REASON>".
print "Error message: " . $e->getMessage() . "\n";
} catch (apiException $e) {
// Other error.
print "An error occurred: (" . $e->getCode() . ") " . $e->getMessage() . "\n";
} catch (Exception $e) {
print "Cannot retrieve the files";
print($e->getMessage());
}
Sorry to ask such a noob question but this is driving me crazy
Thanks
P.S. just for the record, I followed this google-api-php-client/wiki/OAuth2#Service_Accounts
After having a second look to the documentation I found that Google Drive does not support service accounts. I don't know if this is going to change but, with the requirements I have (I need to show in the web is what an end user uploads to a folder (which is public) using the desktop app) I think I cannot use google drive as a valid solution.
I found a solution but I don't like to much since I should have to refresh the token forever Google Drive SDK Service Account Error
I am using gigya php sdk, It works well I am able to post to user's wall or profile using socialise.setStatus method but I am have problems when I try to use the publishuseraction method. I get an error Invalid request signature.
$method = "socialize.publishUserAction";
$request = new GSRequest($apiKey,$secretKey,$method);
$userAction = new GSDictionary("{\"title\":\"This is my title\", \"userMessage\":\"This is a user message\", \"description\":\"This is a description\", \"linkBack\":\"http://google.com\", \"mediaItems\":[{\"src\":\"http://www.f2h.co.il/logo.jpg\", \"href\":\"http://www.f2h.co.il\",\"type\":\"image\"}]}");
$request->setParam("userAction", $userAction);
$request->setParam("uid", $userUID);
$response = $request->send();
if($response->getErrorCode()==0){
echo "Success";
} else {
echo ("Error: " . $response->getErrorMessage());
}
UPDATED AFTER USING $response->getLog()
apiMethod=socialize.publishUserAction
apiKey=correct_api_key
params={"userAction":{},uid":"MY_UID","format":"json","httpStatusCodes":"false"}
URL=http://socialize.gigya.com/socialize.publishUserAction postData=uid=urlencoded(MY_UID)&format=json&httpStatusCodes=false&apiKey=correct_api_keyĆtamp=1296229876&nonce=1.29622987636E%2B12&sig=HEdzy%2BzxetV8QvTDjdsdMWh0%2Fz8%3D
server= web504
I used both put method
$userAction = new GSDictionary();
$userAction->put("title", "This is my title");
$userAction->put("userMessage", "This is my user message");
$userAction->put("description", "This is my description");
$userAction->put("linkBack", "http://google.com");
$mediaItems = array();
$mediaItems[0] = new GSDictionary("{\"src\":\"http://www.f2h.co.il/logo.jpg\", \"href\":\"http://www.f2h.co.il\",\"type\":\"image\"}");
& JSON method
$userAction = new GSDictionary("{\"title\":\"This is my title\", \"userMessage\":\"This is a user message\", \"description\":\"This is a description\",
\"linkBack\":\"http://google.com\", \"mediaItems\":[ {\"src\":\"http://www.f2h.co.il/logo.jpg\", \"href\":\"http://www.f2h.co.il\",\"type\":\"image\"}]}");
I get the same error. And User action is empty using both methods. I appreciate any help.
Thanks.
Try calling getLog() for a bit more information about the error:
if($response->getErrorCode()==0){
echo "Success";
} else {
echo ("Error: " . $response->getErrorMessage());
print "<pre>";
print_r($response->getLog());
print "</pre>";
}
Also, are you able to run getUserInfo w/ the SDK? This is a simple test that also requires a signature. Use the example on the PHP SDK page (about 1/2 way down).