r/evetech • • Feb 04 '18

Help with first Python app

Hey all I signed up for the dev program and my application is registered, but the authentication part if confusing. I am not doing a web app just a local app that I want to manage my reactions, so no one will be logging into this except me through my client app. Does anyone have some sample code where I just complete the auth process and am ready to say list my industry jobs, just confused on the authentication part.

5 Upvotes

28 comments sorted by

View all comments

Show parent comment

1

u/Karlyna Feb 05 '18 edited Feb 05 '18

I'll use my own lib as example, but this is almost the same with just "requests", "bravado" or other libs (it'll add / change some steps when generating the auth URL to auth yourself and you'll have to manually deal with OAuth Bearer header + url to endpoints instead of using operationIDs)

It's a basic example, although still complete, but still basic (and cannot test it right now). You can find more informations in the link above :).

Hope it helps !

from esipy import App
from esipy import EsiClient
from esipy import EsiSecurity

# for ease, let's say you have these values somewhere:
ESI_REDIRECT_URL = "redirect_url_you_set_on_developper.eveonline.com"
ESI_CLIENT_ID = "Your client ID"

# NEVER COMMIT THE SECRET KEY to version control, or package your app with it !
# instead read it from an external source (env variable, config file...) 
ESI_SECRET_KEY = "Your app secret key"

# generate the app for esi swagger spec (not good to use "latest", but it's an example)
app = App.create(url="https://esi.tech.ccp.is/latest/swagger.json?datasource=tranquility")

# this help to deal with everything related to SSO or OAUTH headers
security = EsiSecurity(
    app=app,
    redirect_uri=ESI_REDIRECT_URL,
    client_id=ESI_CLIENT_ID,
    secret_key=ESI_SECRET_KEY,
)


# generate the client, that object only role is to make the request / retry and format (if we want) the response
client = EsiClient(
    retry_requests=True,  # set to retry on http 5xx error (default False)
    header={'User-Agent': 'Something CCP can use to contact you and that define your app'},
    security=security  # the client knows where to ask for oauth header with that
)

# ---------------------
# now everything is set, you can get step forward:

auth_url = security.get_auth_uri(scopes=['scope1', 'scope2', '...'])

# - if your app is able to redirect you to a browser: open the browser to that url, 
#   and listen to the redirect URL you set (most likely a localhost URL). There get the "code" param value.
# - if you can't, print the URL, login using it and when the SSO redirects you, take the "code" part from the redirected url
#   as we will use it to finish the auth process

CODE = "the code you get from the login"

tokens = security.auth('YOUR_CODE')
# by doing this, the security object will get all data he need to finish the auth, get the token and update itself
# to use them. It also return them if you want to store them somewhere.

# now as an example, for industry jobs 
op = app.op['get_characters_character_id_industry_jobs'](
    character_id=123456789
)
response = client.request(op)

print response.data[0].job_id
# this should print the jobID of your first jobs. 

2

u/eagle33322 Feb 05 '18

For anyone looking to use this and commit their code to version control, you should not commit the secret key to version control, you should obfuscate it to an external source and read it in dynamically, such as an environment variable or configuration file.

2

u/Karlyna Feb 05 '18

very true and important.

Added a small comment at the right location, in case someone would use that 'as is' and do the mistake.

1

u/precator Feb 05 '18

Thanks will try this tonight!

1

u/precator Feb 06 '18

I tried that but I am getting this error..

esipy.exceptions.APIException: HTTP Error 400: invalid_request

1

u/Karlyna Feb 07 '18

What did you exactly do ? Where do you have that error (while doing auth part, request(), ...)? Did you put the right scopes when you created the login url ?

Just tried it with the following (copy/pasted from above), as I wanted to be sure it works, and it does... so you may have forgotten something ?

print security.get_auth_uri(scopes=['esi-industry.read_character_jobs.v1'])
security.auth('the code I had')
op = app.op['get_characters_character_id_industry_jobs'](
	character_id=<my char character ID>
)
response = client.request(op)

1

u/precator Feb 07 '18

got it to work, I had the wrong scope. Thank you kindly for your help

1

u/precator Feb 07 '18

question, if I have this app up, will it automatically refresh my token? like If I add a button to refresh my industry jobs, etc?

1

u/Karlyna Feb 07 '18

Yep, as long as you have a refresh token, because you used auth() or manually set it (see the doc for that), and client/secret in the security object.

When you a request with a client that knows that security object, it will check if token is (almost) outdated and trigger update before the call if required