# Authentication Flow - Verify the authorization code

**URL:** <https://forums.eveonline.com/t/authentication-flow-verify-the-authorization-code/68401>\
**Category:** Third Party Developers\
**Created:** [April 7, 2018, 7:49pm UTC](https://forums.eveonline.com/t/authentication-flow-verify-the-authorization-code/68401 "2018-04-07T19:49:45Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steven\_Trux](https://sea1.discourse-cdn.com/eveonline/user_avatar/forums.eveonline.com/steven_trux/32/63146_2.png) [@Steven\_Trux](https://forums.eveonline.com/u/Steven_Trux)\
**Post date:** [April 7, 2018, 7:49pm UTC](https://forums.eveonline.com/t/authentication-flow-verify-the-authorization-code/68401/1 "2018-04-07T19:49:45Z")

</div>

Hi guys, I’m trying to setup SSO in a php project, but I’m stuck at Verify the authorization code.

> $code = $\_GET[‘code’];
> 
> $clientID =  
> $secretKey =  
> $str = $clientID.$secretKey;
> 
> $url = ‘[https://login.eveonline.com/oauth/token](https://login.eveonline.com/oauth/token)’;  
> $auth = base64\_encode($str);
> 
> $curl = curl\_init($url);  
> curl\_setopt($curl, CURLOPT\_POST, true);  
> curl\_setopt($curl, CURLOPT\_HTTPHEADER, array( 'Authorization: Basic '.$auth,  
> ‘Content-Type: application/x-www-form-urlencoded’,  
> ‘Host: [login.eveonline.com](http://login.eveonline.com)’,  
> ‘Content-Length: 0’,  
> ‘grant\_type=authorization\_code&code=’.$code));  
> curl\_exec($curl);  
> $info = curl\_getinfo($curl);  
> curl\_close($curl);  
> var\_dump($info);

What I’m doing wrong?  
Thanks

---

<div class="post-metadata">

**Author:** ![EveDataRules](https://sea1.discourse-cdn.com/eveonline/user_avatar/forums.eveonline.com/evedatarules/32/52946_2.png) [@EveDataRules](https://forums.eveonline.com/u/EveDataRules)\
**Post date:** [April 7, 2018, 8:23pm UTC](https://forums.eveonline.com/t/authentication-flow-verify-the-authorization-code/68401/2 "2018-04-07T20:23:57Z")

</div>

Likely this:

> [@Steven\_Trux](#):
>
> ‘Content-Length: 0’,

Looking further, you are posting `‘grant_type=authorization_code&code=’.$code));` as a header and not the body.

I recommend using a library for oauth2, not rolling your own.

---

<div class="post-metadata">

**Author:** ![Steven\_Trux](https://sea1.discourse-cdn.com/eveonline/user_avatar/forums.eveonline.com/steven_trux/32/63146_2.png) [@Steven\_Trux](https://forums.eveonline.com/u/Steven_Trux)\
**Post date:** [April 7, 2018, 8:49pm UTC](https://forums.eveonline.com/t/authentication-flow-verify-the-authorization-code/68401/3 "2018-04-07T20:49:29Z")

</div>

Any suggestion for a library?

---

<div class="post-metadata">

**Author:** ![EveDataRules](https://sea1.discourse-cdn.com/eveonline/user_avatar/forums.eveonline.com/evedatarules/32/52946_2.png) [@EveDataRules](https://forums.eveonline.com/u/EveDataRules)\
**Post date:** [April 7, 2018, 8:57pm UTC](https://forums.eveonline.com/t/authentication-flow-verify-the-authorization-code/68401/4 "2018-04-07T20:57:53Z")

</div>

I don’t write PHP, but there are a few out there that will likely also depend on you preference for HTTP client. I hear Guzzle is quite good: [https://github.com/kamermans/guzzle-oauth2-subscriber](https://github.com/kamermans/guzzle-oauth2-subscriber)

Look for something that can reuse connections, preferably using HTTP/2.

---

<div class="post-metadata">

**Author:** ![Brock\_Khans](https://sea1.discourse-cdn.com/eveonline/user_avatar/forums.eveonline.com/brock_khans/32/409174_2.png) [@Brock\_Khans](https://forums.eveonline.com/u/Brock_Khans)\
**Post date:** [April 8, 2018, 2:30am UTC](https://forums.eveonline.com/t/authentication-flow-verify-the-authorization-code/68401/5 "2018-04-08T02:30:41Z")

</div>

I used guzzle in all my php code. I found it best to learn guzzle by converting an already working php curl to it. At least thats how I learned it.

I learned it by converting [https://github.com/fuzzysteve/eve-sso-auth](https://github.com/fuzzysteve/eve-sso-auth) to guzzle.

---

<div class="post-metadata">

**Author:** ![Steven\_Trux](https://sea1.discourse-cdn.com/eveonline/user_avatar/forums.eveonline.com/steven_trux/32/63146_2.png) [@Steven\_Trux](https://forums.eveonline.com/u/Steven_Trux)\
**Post date:** [April 8, 2018, 7:14am UTC](https://forums.eveonline.com/t/authentication-flow-verify-the-authorization-code/68401/6 "2018-04-08T07:14:52Z")

</div>

Ok, will have a look. Thanks

---

<div class="post-metadata">

**Author:** ![Snitch\_Ashor](https://sea1.discourse-cdn.com/eveonline/user_avatar/forums.eveonline.com/snitch_ashor/32/470084_2.png) [@Snitch\_Ashor](https://forums.eveonline.com/u/Snitch_Ashor)\
**Post date:** [April 12, 2018, 8:00am UTC](https://forums.eveonline.com/t/authentication-flow-verify-the-authorization-code/68401/7 "2018-04-12T08:00:52Z")

</div>

the way you concatenate secret and key is wrong, they need to be separated by a colon. This is part of my sso class (based on steve ronukens code) that works:

```
            $url = 'https://login.eveonline.com/oauth/token';
            $header = 'Authorization: Basic '.base64_encode(ESI_ID.':'.ESI_SECRET);
            $fields_string = '';
            $fields = array(
                'grant_type' => 'authorization_code',
                'code' => $code,
                );
            foreach ($fields as $key => $value) {
                $fields_string .= $key.'='.$value.'&';
            }
            rtrim($fields_string, '&');
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_USERAGENT, ESI_USER_AGENT);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
            curl_setopt($ch, CURLOPT_POST, count($fields));
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
            $result = curl_exec($ch);

```

Here is the entire class that also does verification and refreshing:

[https://bitbucket.org/snitchashor/spacemail/src/39dbc567a72cc0d6ad2dc122a1b586a5a67ab8be/classes/class.esisso.php?at=master&fileviewer=file-view-default](https://bitbucket.org/snitchashor/spacemail/src/39dbc567a72cc0d6ad2dc122a1b586a5a67ab8be/classes/class.esisso.php?at=master&fileviewer=file-view-default)

---

<div class="post-metadata">

**Author:** ![Steven\_Trux](https://sea1.discourse-cdn.com/eveonline/user_avatar/forums.eveonline.com/steven_trux/32/63146_2.png) [@Steven\_Trux](https://forums.eveonline.com/u/Steven_Trux)\
**Post date:** [April 12, 2018, 7:12pm UTC](https://forums.eveonline.com/t/authentication-flow-verify-the-authorization-code/68401/8 "2018-04-12T19:12:08Z")

</div>

Thank you

---

<div class="post-metadata">

**Author:** ![Whitehound](https://sea1.discourse-cdn.com/eveonline/user_avatar/forums.eveonline.com/whitehound/32/59604_2.png) [@Whitehound](https://forums.eveonline.com/u/Whitehound)\
**Post date:** [April 15, 2018, 5:56pm UTC](https://forums.eveonline.com/t/authentication-flow-verify-the-authorization-code/68401/9 "2018-04-15T17:56:33Z")

</div>

@Snitch_Ashor

```php
            $fields_string = '';
            $fields = array(
                'grant_type' => 'authorization_code',
                'code' => $code,
                );
            foreach ($fields as $key => $value) {
                $fields_string .= $key.'='.$value.'&';
            }
            rtrim($fields_string, '&');

```

This right there can be done better and in several ways. You don’t need to iterate over strings only to concatenate them and then remove trailing characters. You can use `implode()` to do it for you. For example:

```php
$field_string = implode('&', $fields); // $fields as flat array

```

Another way of doing it is to use `http_build_query()` and let it do it for you including any character encoding that may be necessary. For example:

```php
$field_string = http_build_query($fields, '', '&', PHP_QUERY_RFC1738);

```

However, because this isn’t a difficult string to build can you also just write this:

```php
$field_string = 'grant_type=authorization_code&code='.$code;

```

Cheers

By the way, when you want to post some PHP code here in the forum then you can write:

````auto
    ```php
                  ... PHP code goes here ...
    ```

````

This will enable syntax highlighting for PHP.

---

<div class="post-metadata">

**Author:** ![Snitch\_Ashor](https://sea1.discourse-cdn.com/eveonline/user_avatar/forums.eveonline.com/snitch_ashor/32/470084_2.png) [@Snitch\_Ashor](https://forums.eveonline.com/u/Snitch_Ashor)\
**Post date:** [April 15, 2018, 7:14pm UTC](https://forums.eveonline.com/t/authentication-flow-verify-the-authorization-code/68401/10 "2018-04-15T19:14:28Z")

</div>

Thanks, will try http\_build\_query. A simple implode won’t do, as i need key value pairs.

---

<div class="post-metadata">

**Author:** ![Whitehound](https://sea1.discourse-cdn.com/eveonline/user_avatar/forums.eveonline.com/whitehound/32/59604_2.png) [@Whitehound](https://forums.eveonline.com/u/Whitehound)\
**Post date:** [April 15, 2018, 7:26pm UTC](https://forums.eveonline.com/t/authentication-flow-verify-the-authorization-code/68401/11 "2018-04-15T19:26:35Z")

</div>

> [@Snitch\_Ashor](#):
>
> Thanks, will try http\_build\_query. A simple implode won’t do, as i need key value pairs.

I’ve only mentioned it to show there are other ways of doing it. You’d have to use a flat array and not an associative array for `implode()`. It only depends on the what is really necessary to get the job done. I myself use a simple concatenation like in the last example. But I went for JSON encoding. Not sure if that’s better or if URL-query syntax might be a little bit better.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/eveonline/original/3X/4/1/41ec4921cbaca7a6f9ea556e7cc20018461eae5e.png) [@system](https://forums.eveonline.com/u/system)\
**Post date:** [July 14, 2018, 7:26pm UTC](https://forums.eveonline.com/t/authentication-flow-verify-the-authorization-code/68401/12 "2018-07-14T19:26:36Z")

</div>

Closed on July 14, 2018, 7:26pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
