Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting Google REST API through my REST API is not working

I have this application with the below structure enter image description here

I am using rest client library https://github.com/philsturgeon/codeigniter-restclient to connect to MyAPI and using php api client http://code.google.com/p/google-api-php-client/ to connect to Google API

my Controller code is as below

 function index()
    {
        if($this->form_validation->run())
        {
           $logged = $this->rest->get('auth/user',array(
               'email'=>$this->input->post('email')
                   )); 
           var_dump($logged);
        }
        $this->load->view('layout/login',$this->data);
    }

and my API code that handle this request is as below which make sure that user exist in my database and authenticated through Google as well

function user_get()
    {
        $response=NULL;
        $data=array(
            'email'=>$this->get('email')
        );
        $google_account=$this->google->authenticate();
        if(  isset($google_account) && $this->user_model->login($data))
        {
            $response->status='success';
            $response->message=$google_account;
        }
        else
        {
            $response->status='error';
            $response->message='Failed to authenticate user';   
        }
        $this->response($response,200);
    }

and Google library function `Authenticate' is as below

function authenticate()
    {
        $oauth2 = new Google_Oauth2Service($this->client);
        if (isset($_GET['code']))
        {
            $this->client->authenticate($_GET['code']);
            $_SESSION['token'] = $this->client->getAccessToken();
            $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
            header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
            return;
        }
        if (isset($_SESSION['token'])) 
        {
            $this->client->setAccessToken($_SESSION['token']);
        }
        if (isset($_REQUEST['logout'])) 
        {
            unset($_SESSION['token']);
            $this->client->revokeToken();
        }
        if ($this->client->getAccessToken()) 
        {
            $user = $oauth2->userinfo->get();
            // The access token may have been updated lazily.
            $_SESSION['token'] = $this->client->getAccessToken();
            return $user;
        } 
        else 
        {
            $authUrl = $this->client->createAuthUrl();
            redirect($authUrl);
        }

    }

problem is When i connect this throught browser with direct url http://localhost/hac_dc/api/auth/user/[email protected] i get JSON response perfectly

but when i connect it using rest client

i get response false I have tried change the way i use my rest client i tried to add third parameter as JSON and as MIME application/json but didnt work out

I dunno if it's problem or bad practice to connect another REST API though my REST API,

been pulling my hair for hours please help me on this

like image 805
Ahmed Samy Avatar asked Apr 06 '13 18:04

Ahmed Samy


People also ask

How do I connect to a RESTful API?

Step #1 – Enter the URL of the API in the textbox of the tool. Step #2 – Select the HTTP method used for this API (GET, POST, PATCH, etc). Step #3 – Enter any headers if they are required in the Headers textbox. Step #4 – Pass the request body of the API in a key-value pair.


1 Answers

Your REST client doesn't handle the oauth authentication, does it? I assume you shall authenticate your REST client somehow to let it use the access token delivered by Google.

To do so manually, you might save once the access token you've received from Google to a datastore when accessing your API manually (using your browser for instance and by inspecting your browser session). Having this access token available, you could then restore a valid session for your REST client.

Authenticating your REST client towards Google, can be done using cURL or reusing some existing library such as https://github.com/philsturgeon/codeigniter-oauth2.git. To authenticate your REST client towards your own API, you could use HTTP basic/digest authentication (as suggested by CodeIgniter-REST client) after having added an authentication extra-layer to your api (if it is missing).

P.S. When authenticating your user, in case of failure, the 401 response status code might be more legit (see also http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2)

like image 163
Thierry Marianne Avatar answered Sep 22 '22 14:09

Thierry Marianne