2

Setting up a plugin using Pocket API and I'm currently in a authorization loop.

Index template calls the init function. Init function gets requestToken (successfully) and then redirects to Pocket for app authorization (authorizeToken function). After authorization, redirected to callback.

From here, I need to convert the token to a 'Pocket access token', but when the controlller fires it seems to call the init function before the accessToken function and I end up in an endless loop.

Can anyone explain why this is happening? Am I going about this the right way?

Index template:

{% extends "_layouts/cp" %}
{% set title = "Pocket Articles"|t %}

{% set offset = craft.request.getParam('offset') %}
{% set start = craft.pocket.init() %}

{% set content %}
    {{ start }}
{% endset %}

Service:

public function init(){
    $token  = $this->requestToken();
    $auth   = $this->authorizeToken($token);
    return;
}

public function accessToken($code){
    $url                    = $this->getSetting('access_path'); //https://getpocket.com/v3/oauth/authorize
    $requirements['code']   = $code;
    $requirements['key']    = $this->getSetting('consumer_key');

    $curl = $this->initCurl($url, $requirements);
    $results = curl_exec($curl);

    echo "<pre>";
    print_r($results);
    echo "</pre>";

    curl_close($curl);

    return $results;
}

private function authorizeToken($token){
    $auth_path      =$this->getSetting('auth_path'); //https://getpocket.com/auth/authorize?
    $redirect_path  =$this->getSetting('callback_path');
    $actionUrl = UrlHelper::getActionUrl('pocket/callback');


    $url = $auth_path.'request_token='.$token.'&redirect_uri='.$actionUrl;

    header("Location:".$url);

    return $url;
}

Controller:

public function actionCallback(){

    //call pocket server for acccess token
    if(craft()->httpSession->get('code')){
        $code   = craft()->httpSession->get('code');
    }else{
        $code   = "no code";
    }

    $access_connection = craft()->pocket->accessToken($code);
    $variables['access_token'] = $access_connection;

    $this->renderTemplate('pocket/control_panel', $variables);


}

Callback template:

{% extends "_layouts/cp" %}
{% set title = "Pocket Articles"|t %}

{% set offset = craft.request.getParam('offset') %}

{% set content %}
    {{ dump(access_token) }}
{% endset %}
3

The init method is automatically called whenever a service class is instantiated. Think of it as akin to a constructor.

If this is not want you want, just rename the method (to initialize, for example).

  • 1
    I should add that to my bio. – Stephen Lewis Apr 19 '17 at 23:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.