<p><a href="http://www.yiiframework.com/doc/api/1.1/CApplicationComponent#init-detail" rel="nofollow noreferrer">The <code>init</code> method</a> is automatically called whenever a service class is instantiated. Think of it as akin to a constructor.</p>

<p>If this is not want you want, just rename the method (to <code>initialize</code>, for example).</p>

CC BY-SA 3.0
2017-04-19T23:41:06.963Z
I should add that to my bio.
Stephen Lewis
CC BY-SA 3.0
2017-04-19T23:35:59.537Z
2017-04-19T23:35:59.537Z
Stephen Lewis
<p>Setting up a plugin using Pocket API and I'm currently in a authorization loop.</p>

<p>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. </p>

<p>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. </p>

<p>Can anyone explain why this is happening? Am I going about this the right way?</p>

<p><strong>Index template:</strong></p>

<pre><code>{% extends "_layouts/cp" %}
{% set title = "Pocket Articles"|t %}

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

{% set content %}
    {{ start }}
{% endset %}
</code></pre>

<p><strong>Service:</strong></p>

<pre><code>public function init(){
    $token  = $this-&gt;requestToken();
    $auth   = $this-&gt;authorizeToken($token);
    return;
}

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

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

    echo "&lt;pre&gt;";
    print_r($results);
    echo "&lt;/pre&gt;";

    curl_close($curl);

    return $results;
}

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


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

    header("Location:".$url);

    return $url;
}
</code></pre>

<p><strong>Controller:</strong></p>

<pre><code>public function actionCallback(){

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

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

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


}
</code></pre>

<p><strong>Callback template:</strong></p>

<pre><code>{% extends "_layouts/cp" %}
{% set title = "Pocket Articles"|t %}

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

{% set content %}
    {{ dump(access_token) }}
{% endset %}
</code></pre>

<plugin-development><plugins><php>
Pocket API authorization loop
# Pocket API authorization loop

(Asked by: username_0 on 2017-04-19)

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 "";
    print_r($results);
    echo "";

    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 %}

**Tags:** plugin-development, plugins, php


---

## 1 answers

### Answer by username_1 on 2017-04-19. Score: 3 | Accepted answer

[The `init` method](http://www.yiiframework.com/doc/api/1.1/CApplicationComponent#init-detail) 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).

> **Comments:**
> **username_1**: I should add that to my bio.  

---

