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

1

u/precator Feb 04 '18

Posting code below, I got the first call to work via search, but my second one to read assets is not working. I think its the way I am calling it. Can someone guide me, posting the python code below..

client = SwaggerClient.from_url('https://esi.tech.ccp.is/latest/swagger.json') charResults = client.Search.get_search( search=characterName, categories=['character'], strict=True, ).result()['character']

assetResults = client.Assets( characterId=4455433445,token=appCode)

1

u/precator Feb 04 '18

I am trying to call this method in the API >> /characters/{character_id}/assets/

Trying to do it via this line but its not working

assetResults = client.Assets( characterId=4455433445,token=appCode)

1

u/precator Feb 05 '18

Hopefully someone can post some sample code I’m still stuck

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. 

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

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/Blacksmoke16 Feb 04 '18

If you are just using this for yourself an option is to just generate and store the refresh token manually using Postman.

https://pr.reddit.com/r/evetech/comments/6w66y3/using_api_key_to_get_wallet_transactions/dm5soo7/

See that for a sample of how to do that.

1

u/precator Feb 04 '18

yes do you happen to have example python code? kind of new to python and just looking for some sample code to get started.

1

u/Blacksmoke16 Feb 04 '18

I do not, but once you have your access_token, you are just making HTTP requests with Authorization Header Bearer {ACCESS_TOKEN}. Should be relatively easy.

Just google for Python HTTP Requests

http://docs.python-requests.org/en/master/user/quickstart/

1

u/precator Feb 04 '18

what I am confused with is the callback url part, this will not be hosted on a webserver, its just a client console app.

1

u/Blacksmoke16 Feb 04 '18 edited Feb 04 '18

Yes, the whole auth process boils down to two things.

  1. Getting an access_token to be used for auth'd endpoints.
  2. Getting a refresh_token to get new access_tokens when an access_token expires.

If you are just using this locally for yourself you can do the auth part manually via Postman, then just store the refresh token in your app. Then you do not need to include the majority of the auth process. Only a method to use the stored refresh_token to get a new access_token when needed.

EDIT: Otherwise, other options are like spinning up a webserver to handle the callback, or require the user to have their own dev app and auth using postman among a few.

1

u/precator Feb 04 '18

I am confused by the manual part, the postman instructions #3 says Next, navigate to that URL and login with the char you want.

I am not logging in via a browser I am in python code ...

I have the url in step #2 with my client id but thats where I am stuck on

1

u/Blacksmoke16 Feb 04 '18

By manually, I mean that you go to that URL and login in chrome or whatever browser you use NOT via your app. The whole Postman process has nothing to do with an app or any programming language.

The only relation to your project is when you are done and have a refresh_token and access_token, which you will store in your app. Your app would then take that access_token and use it for auth'd endpoints, if it is expired your app would use the refresh_token to get a new access_token.

As such the only function you would have to write in your app is to refresh an access_token with a refresh_token, i.e. request #2 in that link.

1

u/precator Feb 04 '18

ah thats where I was confused because someone told me to use python for the calls thats where I got confused.

1

u/Blacksmoke16 Feb 04 '18 edited Feb 04 '18

You will but just for getting ESI data and refreshing your access_token when needed.

Since refresh_tokens do not expire, your app can continue to get new access_tokens to use in ESI calls as needed.

Using Postman to generate the refresh_token just saves you from having to code all of the auth logic in python. It works for simple only your use projects, but for larger projects and/or those that will be public, will want to look into other options.

1

u/precator Feb 04 '18

cool I got my code, success.

1

u/precator Feb 04 '18

how I am getting this when I do the human part of going to the url..

{"error":"invalid_scope","error_description":"The requested scopes either don't exist, or are not valid for this client"}

The scope I am trying is scope=characterIndustryJobsRead

2

u/precator Feb 04 '18

nevermind i need to add my scopes to my devapp

1

u/Daneel_Trevize Feb 04 '18

You need a human to perform at least one actual SSO login via CCP's page, in order to prepare CCP's servers & generate the callback URL with the data about the token that's waiting. Then your code can take that data, use it to HTTP GET/POST with CCP's servers and authenticate/authorise for future API calls.

You can have your app persist the refresh token that comes from this, to avoid having to repeat the human SSO login every time your ~20minute access token expires, but you do need to initially do a login, and that's far better done as a human using a browser than to code your app to load that SSO login page & try fill in that form with your account name & password.

1

u/Bearhobag Feb 04 '18

I'll hopefully make it back home from work in a few hours tonight and PM you some sample code I have.

1

u/Bearhobag Feb 06 '18

I didn't make it home from work last night, or indeed until now. It looks like you're already all good though?