I have the following code:
$twitter = new Zend_Service_Twitter(array('username' => $this->site->twitter_username, 'accessToken' => $this->site->twitter_password));
$response = $twitter->account->verifyCredentials();
print_r($response);
$twitter->account->endSession();
My username is my Login Username on twitter, my $this->site->twitter_password is my Access Token (oauth_token)
Yet I get:
Zend_Rest_Client_Result Object ( [_sxml:protected] => SimpleXMLElement Object ( [request] => /account/verify_credentials.xml [error] => Could not authenticate you. ) [_errstr:protected] => )
I'm unsure where I'm going wrong, any ideas?
I recently blogged about this and have included some instructions that should get you going, let me know if you need any more pointers.
PHP Using Zend framework to display new tweets as KDE notifications
Related
OK, I'm trying to use Hybridauth with laravel 4. However I seem to be getting the very common when trying to log in with facebook:
Authentication failed! Facebook returned an invalid user id.
I have read all the other posts, and have had no luck, so just hoping someone may be able to help me.
I followed this tutorial: http://www.mrcasual.com/on/coding/laravel4-package-management-with-composer/
And have tried several other configurations to no success.
Here is my config/hybridauth.php
<?php
return array(
"base_url" => "http://myapp.dev/social/auth/",
"providers" => array (
"Facebook" => array (
"enabled" => true,
"keys" => array ( "id" => "****", "secret" => "****" ),
),
),
);
And here is my route:
Route::get('social/{action?}', array("as" => "hybridauth", function($action = "")
{
// check URL segment
if ($action == "auth") {
// process authentication
try {
Hybrid_Endpoint::process();
}
catch (Exception $e) {
// redirect back to http://URL/social/
return Redirect::route('hybridauth');
}
return;
}
try {
// create a HybridAuth object
$socialAuth = new Hybrid_Auth(app_path() . '/config/hybridauth.php');
// authenticate with Facebook
$provider = $socialAuth->authenticate("Facebook");
// fetch user profile
$userProfile = $provider->getUserProfile();
}
catch(Exception $e) {
// exception codes can be found on HybBridAuth's web site
return $e->getMessage();
}
// access user profile data
echo "Connected with: <b>{$provider->id}</b><br />";
echo "As: <b>{$userProfile->displayName}</b><br />";
echo "<pre>" . print_r( $userProfile, true ) . "</pre><br />";
// logout
$provider->logout();
}));
So, when I access "myapp.dev/social" I'm brought to the facebook sign up page everthing seems to work fine, asks me to allow permissions to myadd.dev. After I click OK I am brought to the following URL: http://myapp.ie/social#_=_ where the error is displayed.
Not sure if this is relevant:
Just from observing other sites that in-cooperate a facebook login.. the redirect URL looks something like http://somesite.dev/subdomain/#_=_ . In other words they have a slash before the #=. Is this my problem, how do I fix it?? Very new to hybridauth so any help greatly appreciated thanks.
Oh I do realize that this post is very similar to other posts but I have yet to find a solution.
UPDATE: the exact error: Authentification failed. The user has canceled the authentication or the provider refused the connection.
In base_facebook.php do following
public static $CURL_OPTS = array(
CURLOPT_CONNECTTIMEOUT => 50,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60,
CURLOPT_USERAGENT => 'facebook-php-3.2',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
);
protected $trustForwarded = true;
protected $allowSignedRequest = false;
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
at modules/hybridauth/Hybrid/thirdparty/Facebook/base_facebook.php:128
solved!
For anyone else this is what worked for me: I reset app secret and now works great. No idea why my first app secret key did not work. Spent a ridiculous amount of time trying to fix this error.
Had this error in the past. Solved by modyfying Hybridauth's code myself.
In thirdparty/Facebook/base_facebook.php make sure $CURL_OPTS array uses:
CURLOPT_SSL_VERIFYPEER => false
In my case I was closing session files for performance improvements so I added:
session_start()
inside Storage.php wherever HA::STORE session var is being updated/unset.
Let me know if it helps.
CURLOPT_SSL_VERIFYPEER => false & resetting my app secret key didn't work for me. I was getting this error because of some conflict with privileges I had previously setup. Removing the app from my facebook account did the trick (under privacy settings -> apps).
REMOVE THE TRAILING SLASH !!! (in config/hybridauth.php)
"base_url" => "http://myapp.dev/social/auth/",
should be
"base_url" => "http://myapp.dev/social/auth",
My case was a little bit more specific, but just in case: Be carefull with redirects!
I had an SSL Certificate installed and a redirect to force the user over https, but when I first configured HybridAuth I didn't took this into account. The facebook request was being redirected over to https causing the $_REQUEST data to be lost in the process.
For me the change was, in Hybrid/config.php:
"base_url" => "http://my-site.com/"
to
"base_url" => "https://my-site.com/"
I was having the same issue (although using HybridAuth on Yii) and turns out my app on Facebook was still in Sandbox mode. No source code changes needed on HybridAuth, just needed to turn off Sandbox Mode for the app and suddenly everything worked. Hope this helps.
This happened to me because my SSL is terminated in AWS's load balancer
Just update the config file in your app/config to include the trustForwarded setting
<?php
return array(
'base_url' => 'http://website.com/oauth/auth',
'providers' => array (
'Facebook' => array (
'enabled' => true,
'keys' => array ( 'id' => 'redacted', 'secret' => 'redacted' ),
'trustForwarded' => true,
),
),
);
I had the exact same error message on a wordpress installation using Hybridauth. To find the problem I set up an isolated test with the Facebook PHP SDK (which Hybridauth uses) just to find out that curl_exec was not enabled on my host. Happily, an easy fix.
If you are on apache open you php.ini and delete curl_exec from this line:
disable_functions = curl_exec
Reload your apache configuration and voila :)
Hope this will help somebody.
I've upgraded to Zend Framework v1.12.3 because it supports the TwitterAPI v1.1 with Zend_Service_Twitter. Before, I've used the TwitterAPI 1.0 (prior ZF 1.12.3) which works well, but that's gonna change in march 2013.
If I call the TwitterAPI v1.1 with the following ZF 1.12.3 code, I keep getting a strange error which I can't explain to myself:
Code:
$twitterService = new Zend_Service_Twitter(array(
'consumerKey' => $this->config['consumerKey'],
'consumerSecret' => $this->config['consumerSecret'],
'username' => $twitterVO->getTwitterUserName(),
'accessToken' => $accessToken // unserialized object
));
$response = $twitterService->statusesUpdate("TEST");
And that's the error which I get. It doesn't matter which function I call (in this case it's statusesUpdate("Test")):
The message is "Invalid chunk size "" unable to read chunked body". The "type" attribute within the array shows a "->". That's also a bit suspect but I couldn't find out where it comes from.
Does anyone have a working example with Zend Framework 1.12.3?
Since ZF 1.12, the Twitter app Oauth parameters can be given in the Zend_Service_Twitter constructor, but consumerKey and consumerSecret must go under the "oauthOptions" array.
This works for me:
$accessToken = new Zend_Oauth_Token_Access();
$accessToken->setToken('YourAccessToken');
$accessToken->setTokenSecret('YourAccessTokenSecret');
$twitter = new Zend_Service_Twitter(
array(
'username' => 'YourUsername',
'accessToken' => $accessToken,
'oauthOptions' => array(
'consumerKey' => 'YourConsumerKey',
'consumerSecret' => 'YourConsumerSecret'
)
)
);
$result = $twitter->statusesUserTimeline('TEST');
I am storing the linkedin access token in database. This is the token that is stored in my database:
oauth_token=xxxxxxxxxxxxxxxxxxx&oauth_token_secret=xxxxxxxxxxxxxxxxxxx&oauth_expires_in=5183998&oauth_authorization_expires_in=5183998
I want to retrieve the linkedin connections using this access token. I am trying to make a call like this:
$a = new Zend_Oauth_Token_Access();
$client = $a->getHttpClient( array(
'siteUrl' => LIN_SITE_URL,
'callbackUrl' => LIN_SITE_CALLBACK_URL,
'requestTokenUrl' => LIN_REQUEST_TOKEN_URL,
'userAuthorizationUrl' => LIN_USER_AUTHORIZATION_URL,
'accessTokenUrl' => LIN_ACCESS_TOKEN_URL,
'consumerKey' => LIN_CONSUMER_KEY,
'consumerSecret' => LIN_CONSUMER_SECRET
) );
$client->setUri('http://api.linkedin.com/v1/people/~/connections:(id,first-name,last-name,picture-url)');
$client->setParameterGet('token',$linToken);
$client->setMethod(Zend_Http_Client::GET);
$response = $client->request();
$content = $response->getBody();
$data = json_decode(Zend_Json::fromXml($content, false));
print_r($data); echo "<br/>";
The error i am getting here is:
stdClass Object ( [error] => stdClass Object ( [status] => 404 [timestamp] => 1349429996351 [request-id] => 8U8A1UNF1V [error-code] => 0 [message] => Could not find person based on: ~ ) )
Is this the correct way to make a call or am i doing something wrong here. I am using zend framework.
Thanks.
I was able to solve the problem by passing the outh_token and outh_token_secret as an array to the setParams() of Zend_Oauth_Token_Access as below:
$a = new Zend_Oauth_Token_Access;
$a->setParams(array(
'oauth_token' => 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
'oauth_token_secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
));
In your code example I don't see where you're setting the access token and secret. I just see that you're instantiating the $a variable:
$a = new Zend_Oauth_Token_Access();
Are you retrieving the access token and secret from you database then setting it to the $a variable? Something like this:
$a->setToken($row['token'])
->setTokenSecret($row['secret']);
By the way, this is a good reference for using Zend and LinkedIn: http://www.contentwithstyle.co.uk/content/linkedin-and-zendoauth/
The following PHP code uploads a new track to SoundCloud successfully, but the tweet is not sent.
Is there something I need to have in there as well in order to do this?
$track = $soundcloud->post('tracks',
array(
'track[asset_data]' => '#audio.mp3',
'track[title]' => "my audio",
'track[description]' => "Updated: " . date('l jS F Y h:i:s A'),
'track[sharing]' => 'public',
'track[shared_to][connections][][id]' => '123',
'track[sharing_note]' => 'Have a listen to'
));
Also I'd like to be able to disable comments on the audio I upload, but I wasn't sure what the parameter for that would be too?
Thanks!
dB
I'm unable the repro the sharing problem. Please note that sometimes sharing on other social networks doesn't happen right away. Are you still having trouble? Here's the code I used:
<?php
require_once 'Services/Soundcloud.php';
$client = new Services_Soundcloud("foo", "bar");
$client->setAccessToken('ACCESS_TOKEN');
$track = $client->post('tracks', array(
'track[title]' => 'Foooo',
'track[asset_data]' => '#/Users/paul/audio.wav',
'track[sharing]' => 'public',
'track[shared_to][connections][][id]' => 'CONNECTION_ID',
'track[sharing_note]' => 'Check it out'
));
print_r($track);
Also verify that your CONNECTION_ID is correct. Some code to get a list of connections so you can verify the id:
<?php
require_once 'Services/Soundcloud.php';
$client = new Services_Soundcloud("foo", "bar");
$client->setAccessToken('ACCESS_TOKEN');
print_r(json_decode($client->get('me/connections')));
Unfortunately there's no way currently to disable comments via the API. I'll file a bug and see about getting this fixed.
Hope that helps!
Answer + new question
I found out that the code below works just fine on a LIVE server. LinkedIN blocked all requests from localhost.
That established; Does anybody know how to test an application from localhost with LinkedIN OAuth? Because doing this on a live server sucks!
Old Question
I'm trying to connect with Zend_OAuth to LinkedIN. This code used to work, but now it returns an error in http request while I'm trying to retrieve an access token.
Tried checking the LinkedIN api, but the code still seems valid. Tried several scripts but all with the same result.
The config is setup in the preDispatch of my controller
$this->configLinkedin = array(
'version' => '1.0',
'siteUrl' => 'http://'.$_SERVER['HTTP_HOST'].$this->view->baseUrl(false).'/news/index/connectlinkedin',
'callbackUrl' => 'http://'.$_SERVER['HTTP_HOST'].$this->view->baseUrl(false).'/news/index/connectlinkedin',
'requestTokenUrl' => 'https://api.linkedin.com/uas/oauth/requestToken',
'userAuthorisationUrl' => 'https://api.linkedin.com/uas/oauth/authorize',
'accessTokenUrl' => 'https://api.linkedin.com/uas/oauth/accessToken',
'consumerKey' => 'XXX',
'consumerSecret' => 'XXX'
);
And the code in the action to connect to linkedIN is
$this->consumer = new Zend_Oauth_Consumer($this->configLinkedin);
if(!empty($_GET) && isset($_SESSION['LINKEDIN_REQUEST_TOKEN']))
{
$token = $this->consumer->getAccessToken($_GET, unserialize($_SESSION['LINKEDIN_REQUEST_TOKEN']));
// Use HTTP Client with built-in OAuth request handling
$client = $token->getHttpClient($this->configLinkedin);
// Set LinkedIn URI
$client->setUri('https://api.linkedin.com/v1/people/~:(id,first-name,last-name,picture-url)');
// Set Method (GET, POST or PUT)
$client->setMethod(Zend_Http_Client::GET);
// Get Request Response
$response = $client->request();
$this->NewsService->TokenSocialMedia(
$token,
'linkedin',
serialize($response->getBody())
);
$_SESSION['LINKEDIN_REQUEST_TOKEN'] = null;
$this->_helper->flashMessenger(array('message' => $this->view->translate('The CMS is successfully connected to your linkedin account'), 'status' => 'success'));
$this->_helper->redirector('settings#settingSocial', 'index');
}
else
{
$token = $this->consumer->getRequestToken();
$_SESSION['LINKEDIN_REQUEST_TOKEN'] = serialize($token);
$this->consumer->redirect();
}
What am I missing or doing wrong? I use a similair setup for Twitter and that works fine.
UPDATE 20 September 211
I found out that this rule is returning the error:
$token = $this->consumer->getRequestToken();
I'm still clueless why, and reading the linkedin api doesn't help a bit. Will keep you posted.
I got similar problem and after adding openssl extension it was solved
try adding to php.ini this line:
extension=php_openssl.dll
I got the same issue, try to turn off ssl before asking the new consumer :
$httpConfig = array(
'adapter' => 'Zend\Http\Client\Adapter\Socket',
'sslverifypeer' => false
);
$httpClient = new HTTPClient(null, $httpConfig);
OAuth::setHttpClient($httpClient);