Goodreads Developers discussion

46 views
questions > Wordpress Social Login app oAuth Error




Comments (showing 1-3)    post a comment »
dateUp_arrow    newest »

message 3: by Reid (last edited May 22, 2012 06:20AM) (new)

Reid Crandall (reidcrandall) | 2 comments Yes. I am using PHP.

The Wordpress Social Login oAuth for Goodreads looks like this:

<?php
/*!
* HybridAuth
* http://hybridauth.sourceforge.net | https://github.com/hybridauth/hybridauth
* (c) 2009-2011 HybridAuth authors | hybridauth.sourceforge.net/licenses.html
*/

/**
* Hybrid_Providers_Goodreads
*/
class Hybrid_Providers_Goodreads extends Hybrid_Provider_Model_OAuth1
{
/**
* IDp wrappers initializer
*/
function initialize()
{
parent::initialize();

// provider api end-points
$this->api->api_base_url = "http://www.goodreads.com/";
$this->api->authorize_url = "http://www.goodreads.com/oauth/authorize";
$this->api->request_token_url = "http://www.goodreads.com/oauth/reques...";
$this->api->access_token_url = "http://www.goodreads.com/oauth/access...";

// turn off json parsing!
$this->api->decode_json = false;
}

/**
* finish login step
*/
function loginFinish()
{
// in case we get authorize=0
if ( ! isset($_REQUEST['oauth_token']) || ( isset( $_REQUEST['authorize'] ) && $_REQUEST['authorize'] == "0" ) ){
throw new Exception( "Authentification failed! The user denied your request.", 5 );
}

$oauth_verifier = @ $_REQUEST['oauth_token'];

if ( !$oauth_verifier ){
throw new Exception( "Authentification failed! {$this->providerId} returned an invalid oauth verifier.", 5 );
}

// request an access token
$tokens = $this->api->accessToken( $oauth_verifier );

// access tokens as recived from provider
$this->access_tokens_raw = $tokens;

// check the last HTTP status code returned
if ( $this->api->http_code != 200 ){
throw new Exception( "Authentification failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ), 5 );
}

// we should have an access_token, or else, something has gone wrong
if ( ! isset( $tokens["oauth_token"] ) ){
throw new Exception( "Authentification failed! {$this->providerId} returned an invalid access token.", 5 );
}

// we no more need to store requet tokens
$this->deleteToken( "request_token" );
$this->deleteToken( "request_token_secret" );

// sotre access_token for later user
$this->token( "access_token" , $tokens['oauth_token'] );
$this->token( "access_token_secret" , $tokens['oauth_token_secret'] );

// set user as logged in to the current provider
$this->setUserConnected();
}

/**
* load the user profile from the IDp api client
*/
function getUserProfile()
{
$response = $this->api->get( 'http://www.goodreads.com/api/auth_user' );

// check the last HTTP status code returned
if ( $this->api->http_code != 200 )
{
throw new Exception( "User profile request failed! {$this->providerId} returned an error: " . $this->errorMessageByStatus( $this->api->http_code ), 6 );
}

// parse the response
$response = @ new SimpleXMLElement( $response );

$this->user->profile->identifier = (string) $response->user['id'];
$this->user->profile->displayName = (string) $response->user->name;
$this->user->profile->profileURL = (string) $response->user->link;

// try to grab more information about the user if possible
$response = $this->api->get( 'http://www.goodreads.com/user/show/' . $this->user->profile->identifier . '.xml' );

// check the last HTTP status code returned
if ( $this->api->http_code != 200 )
{
return $this->user->profile;
}

// parse the response
$response = @ new SimpleXMLElement( $response );

$this->user->profile->photoURL = (string) $response->user->image_url;
$this->user->profile->webSiteURL = (string) $response->user->website;
$this->user->profile->description = (string) $response->user->about;
$this->user->profile->country = (string) $response->user->location;
$this->user->profile->gender = (string) $response->user->gender;
$this->user->profile->age = (string) $response->user->age;

return $this->user->profile;
}
}

The code I've got right now is basically me starting over from scratch. I've tried a bunch of different things and since I'm so new at this, I don't know enough to know if any of them were even close. And there aren't a lot of PHP examples around...

In it, I've replaced MYKEY with the actual dev key. I'm not sure how exactly to hook on to the oAuth done in the Wordpress Social Login, so that I can access the oAuth Tokens once the user has logged in to the site.

<a href="<?php $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, 'http://www.goodreads.com/shelf/add_to...');
curl_exec($curl);
curl_close($curl); ?>">Read</a>

When I click the link, I am redirected back to the same page with an additional /not%20authorized at the end of the address.

Thanks!


message 2: by Ettore, Software Engineer (new)

Ettore Pasquini | 142 comments Mod
Hi Reid,
Are you using PHP? Can you post your code here?


Reid Crandall (reidcrandall) | 2 comments I am able to log into the site I'm working on using Goodreads at www.susancrandall.net, but when I try to re-check authentication for use with the API, I get an error on goodreads. http://www.goodreads.com/oauth/author.... Anyone have any ideas?

I am new at this.


back to top
unread topics | mark unread