An error has occurred

Hello there,

I have started using (trying to) the new API, and I am a bit confused as I am getting an error which is not an error. I am running the following code in my machine:

$bodyData = array(
     "grant_type" => "authorization_code",
     "code" => $code);

 $headerData = array(
    "Authorization:Basic " . base64_encode(clientID.":".secretKey),
    "Content-Type:application/json",
    'Host:login.eveonline.com'
 );

 $curl = curl_init();

 $url = 'https://login.eveonline.com/v2/oauth/token/';

 $certificate = "C:\wamp64\certs\cacert.pem";
 curl_setopt($curl, CURLOPT_CAINFO, $certificate);
 curl_setopt($curl, CURLOPT_CAPATH, $certificate);

 curl_setopt($curl, CURLOPT_URL, $url);
 curl_setopt($curl, CURLOPT_POST, true);
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($bodyData));
 curl_setopt($curl, CURLOPT_HTTPHEADER, $headerData);

 $response = curl_exec($curl);
 if( $response === false){
     echo 'Curl error: ' . curl_error($curl);
 }
 else{
     echo $response;
 }

This was supposed to complete the step two in the authorization but, although the curl command is not generating any error, the response I am getting is:

{ "Message" : "An error has occurred." }

I am not sure what I am doing wrong and unfortunately Google has not been of great help until now.

I tried to run the same from the command line as

curl -XPOST -H "Content-Type:application/json" -H "Authorization:Basic ZWRlODdiOWIzZGMyNGR...SE1mREpYTFlDNWVjRkptSGQwMFlzWUc4MXpETU9vcDZIYmZYeg==" -d '{"grant_type":"authorization_code", "code":"DKVHTD6MQXt-t9YNe4SVD8ATe...mwl0NBVo35uImZG9IxbHhhjda"}' https://login.eveonline.com/v2/oauth/token

and got exactly the same response.

Any hint would be appreciated.

Thanks a lot!

GK

See the docs on how to make these requests: https://docs.esi.evetech.net/docs/sso/web_based_sso_flow.html.

My first guess is you’re using JSON, when it should be application/x-www-form-urlencoded.

1 Like

That solved the issue; I was looking at old documentation. That has brought another problem

{"error":"invalid_grant","error_description":"Grant type is not supported."}

but I hope to get it done reading the correct docs.

Thanks a lot!

GK

@Blacksmoke16, sorry to bother you. I was not able to find any solution re the second error. Not really sure which grant is the one giving the error.

Can you share your latest code/request?

This?

https://login.eveonline.com/oauth/authorize?response_type=code&redirect_uri=http%3A%2F%2Flocalhost%2Foauth-callback%2Findex.php&client_id=ede87b9b3dc24dae91c9d991ed481dc6&scope=esi-markets.structure_markets.v1%20esi-assets.read_corporation_assets.v1&state=QZZmcf1Nr6

Well, not quite, but I think that actually shows the problem. Carefully read thru https://docs.esi.evetech.net/docs/sso/web_based_sso_flow.html and make sure you’re following the steps. Notice that the URLs are different than what you’re using. I.e. you want to use /v2/oauth/authorize/ and /v2/oauth/token.

That v2 slipped through the cracks in the first call. Thanks for that.

However, /v2/oauth/token is the one to transform the code I get into a token.

As I understood it, first I have to call /v2/oauth/authorize and when I get the code, then I use this code to pass it to /v2/oauth/token together with the secret key, client ID, etc, … as in the code I shared in the first post.

That is what I understood from the page you sent me, which I have almost memorized :smiley:

First I call https://login.eveonline.com/v2/oauth/authorize?response_type=code&redirect_uri=http%3A%2F%2Flocalhost%2Foauth-callback%2Findex.php&client_id=ede87b9b3dc24...9d991ed481dc6&scope=esi-markets.structure_markets.v1%20esi-assets.read_corporation_assets.v1&state=FhBvVotmtFJjUrsgpmi3nMCGHoJkTnhH

and get

http://localhost/oauth-callback/index.php?code=8VmoXRWTWkC3LLjL38qnaA&state=FhBvVotmtFJjUrsgpmi3nMCGHoJkTnhH

and I use the code to call /v2/oauth/token, right?

Yes, you use that code to get a token. Starting on step 4 on that guide ^.

Hey @Gimhae_Kim, are you aware of https://esi.evetech.net/ui/ and the capabilities of the API as well how to use it?

@Rtg_Quack I was indeed. Thanks.

It was me forgetting how to do PHP no more.

Thank you both!

GK

If you use PHP to connect to ESI I might suggest to use a robust OAUTH2 implementation like Evelabs\OAuth2
I use it in my own project, see in particular method eveSsoLogin() in https://github.com/chrRtg/eve-skylizer/blob/master/module/User/src/Service/EveSSOManager.php

Hope it helps :slight_smile:

1 Like

There seem to 5 or six folks on the planet who can make ESI work. So it’s a good thing they broke my spreadsheets to those 5 dudes can do the exact same thing they were dong with CREST, which was mostly usable by the average person.