Question

Generating Clients/SDKs Using Swagger Codegen

  • 18 November 2015
  • 44 replies
  • 1233 views

  • Anonymous
  • 0 replies

The Looker API v3.0 has a custom authentication scheme which requires a SHA-1 hash of the query parameters. Does the generated code know how to do this or I need to modify the generated code.


44 replies

Hey @svarun ,


It sounds like you might have a firewall between your ssh host and github that is blocking traffic on port 22. Try from a location outside of your internal network.


Cheers,

William

I was able to get the Swagger Codegen to generate the C# .Net implementation of the client. It’s important to note that I had to add the authorization token to the Authorization header parameter. And add it to the default header (see below).


`    class Program
{
static void Main(string[] args)
{
string clientId = "client id";
string clientSecret = "secret";

IO.Swagger.Api.ApiAuthApi authApi = new IO.Swagger.Api.ApiAuthApi();
AccessToken token = authApi.Login( clientId, clientSecret);

// set the default authorization header to 'token <your token>'
// this will add as a default header and be sent on all API calls
ApiClient client = authApi.ApiClient;
client.AddDefaultHeader("Authorization", "token " + token.Token);

UserApi userApi = new UserApi(client);
User meUser = userApi.Me("first_name,last_name, email");

string output = authApi.Logout();
}
}
`

Not being familiar with using OAuth 2.0 and curl, the setup steps to get swagger.json took a bit of figuring out.

In case anyone else is experiencing similar difficulties:



  1. Get the access token:

    curl -X POST -d ‘client_id=Pxg…&client_secret=sdfo…’ https://mycompany.looker.com:19999/login

    -> returns {“access_token”:“bhg5…”,“token_type”:“Bearer”,“expires_in”:3600}

  2. Use the access token to get the resource of interest

    curl -H ‘Authorization: Bearer bhg5…https://mycompany.looker.com:19999/api/3.0/swagger.json

    -> returns swagger.json


(Possibly optional Step 0: some fiddling with ca-cert location on Ubuntu to get curl https working:

sudo ln -s /etc/ssl/certs/ca-certificates.crt /etc/pki/tls/certs/ca-bundle.crt

but that could be machine-specific)

Didn’t try other looks methods…need a look ID for the other and all_looks() appeared to be the most basic. Any others you suggest I try?


When I change the port in the python script from 19999 to 9999 the call to login fails with a 403 error. Not sure what you mean by “put that port in your api endpoint”.


From what I’ve gathered from your article API communication is over 19999, but typical Looker traffic is over 9999. Or am I misunderstanding?

login can’t even connect when I drop the port:


2016-02-23 12:59:28,969 WARNING Retrying (Retry(total=2, connect=None, read=None, redirect=None)) after connection broken by ‘NewConnectionError(’<urllib3.connection.VerifiedHTTPSConnection object at 0x10b38ed50>: Failed to establish a new connection: [Errno 61] Connection refused’,)’: /login?client_secret=hidden&client_id=hidden


Did I miss some configuration step?

Fixed it by appending /api/3.0 to base_url e.g. base_url = https://looker.customer.com:19999/api/3.0.


Sorry for any confusion I may have created.

Hi,


I’m running into the same issue as Scott had above, but appending /api/3.0 to my base_url didn’t work and I’m still getting the 404 errors. Any ideas?


Thanks!


-Grayson

Grayson-Williamss-Macbook-Pro:rsdw.old grayson$ python looker_test.py

Traceback (most recent call last):

File “looker_test.py”, line 17, in

token = unauthenticated_authApi.login(client_id=client_id, client_secret=client_secret)

File “build/bdist.macosx-10.11-intel/egg/swagger_client/apis/api_auth_api.py”, line 122, in login

File “build/bdist.macosx-10.11-intel/egg/swagger_client/api_client.py”, line 322, in call_api

File “build/bdist.macosx-10.11-intel/egg/swagger_client/api_client.py”, line 149, in __call_api

File “build/bdist.macosx-10.11-intel/egg/swagger_client/api_client.py”, line 358, in request

File “build/bdist.macosx-10.11-intel/egg/swagger_client/rest.py”, line 208, in POST

File “build/bdist.macosx-10.11-intel/egg/swagger_client/rest.py”, line 177, in request

swagger_client.rest.ApiException: (404)

Reason: Not Found

HTTP response headers: HTTPHeaderDict({‘Content-Type’: ‘application/json;charset=utf-8’, ‘Content-Length’: ‘69’, ‘Vary’: ‘Accept-Encoding’, ‘X-Content-Type-Options’: ‘nosniff’})

HTTP response body: {“message”:“Not found”,“documentation_url”:“http://docs.looker.com/”}


Grayson-Williamss-Macbook-Pro:rsdw.old grayson$

Here you go. I am using the API keys from my user page in Admin.


preliminaries


import swagger_client as looker

import os


dir(looker) # to see available methods

base_url = “https://looker.mycompany.com:19999

#base_url = “https://looker.mycompany.com:19999/api/3.0

client_id = os.environ.get(‘LOOKER3_KEY’)

client_secret = os.environ.get(‘LOOKER3_SECRET’)


instantiate Auth API


unauthenticated_client = looker.ApiClient(base_url)

unauthenticated_authApi = looker.ApiAuthApi(unauthenticated_client)


authenticate client


token = unauthenticated_authApi.login(client_id=client_id, client_secret=client_secret)

client = looker.ApiClient(base_url, ‘Authorization’, 'token ’ + token.access_token)


instantiate Look API


looks = looker.LookApi(client)

I tried hardcoding them and that got me farther, but now when I try to run a look I get:


Grayson-Williamss-Macbook-Pro:rsdw.old grayson$ python looker_test.py 
Traceback (most recent call last):
File "looker_test.py", line 23, in <module>
my_1235 = looks.run_look(1235, 'json')
File "build/bdist.macosx-10.11-intel/egg/swagger_client/apis/look_api.py", line 439, in run_look
File "build/bdist.macosx-10.11-intel/egg/swagger_client/api_client.py", line 322, in call_api
File "build/bdist.macosx-10.11-intel/egg/swagger_client/api_client.py", line 149, in __call_api
File "build/bdist.macosx-10.11-intel/egg/swagger_client/api_client.py", line 342, in request
File "build/bdist.macosx-10.11-intel/egg/swagger_client/rest.py", line 184, in GET
File "build/bdist.macosx-10.11-intel/egg/swagger_client/rest.py", line 177, in request
swagger_client.rest.ApiException: (404)
Reason: Not Found
HTTP response headers: HTTPHeaderDict({'Vary': 'Accept-Encoding', 'Content-Type': 'application/json;charset=utf-8', 'Content-Length': '69', 'X-Cascade': 'pass', 'X-Content-Type-Options': 'nosniff'})
HTTP response body: {"message":"Not found","documentation_url":"http://docs.looker.com/"}

Grayson-Williamss-Macbook-Pro:rsdw.old grayson$
Userlevel 3
Badge

Hey there @cross!


I normally see this error when I’ve made a mistake in the permissions and groups that I’ve given to the user that I am authenticating as. This can also come up when the optional arguments provided to the function are not as expected.


I’d try these two steps first:



  1. Sudo as the API user and make sure you can see and run the Look in the front-end application. If you can’t, then you must adjust their permissions or access so that they can find and run the Look.

  2. EDIT: Nevermind, data format argument is only optional for other endpoints, required for this one.


If you still can’t run the Look from the API, try authenticating and running the Look via curl. If you can, this implies that maybe authentication is not working as expected in your Python script.

Userlevel 3
Badge

That basic syntax works for me, this worked on looker.learn.com:


curl -i -H "Authorization: token <BIG_TOKEN_HERE>" https://learn.looker.com:19999/api/3.0/looks/2938/run/json

HTTP/1.1 200 OK
Content-Type: application/json;charset=utf-8
Vary: Accept-Encoding
X-Content-Type-Options: nosniff
Transfer-Encoding: chunked

[{"users.age_tier":"80 or Above","order_items.count":1984,"order_items.percent_total_gm":4.8631375010242754},
. . .

Are you still getting a 404?

Userlevel 3
Badge

@cross no problem! Glad to hear it’s working now 😃

Userlevel 3
Badge

👍

Userlevel 2

Take a look at Step 2 under the “Setup” section.


https://<your_looker_endpoint>:19999/api/3.0/swagger.json


You can retrieve the JSON from the above-mentioned endpoint.

Hello there!


I’m having a similar problem to @scottmdavidson. But, my base_url is already correct syntax. I’m getting an ApiException: (400) error. It only happens when using functions whose method in the response variable is ‘POST’ (create_user, create_user_access_filter). I am able to run anything else that I’ve tried (all_users, all_user_access_filters).


What I mean by response variable whose method is post:



    response = self.api_client.call_api(resource_path, 'POST',
path_params,
query_params,
header_params,
body=body_params,
post_params=form_params,
files=local_var_files,
response_type='User',
auth_settings=auth_settings,
callback=params.get('callback'))


Any thoughts? Thanks!

Solved - disregard 🙂

@svarun


You could indeed use the github site and hit “Clone or Download”:




I hope this helps!

Could you come on chat and we’ll work on this issue with you @svarun? Either that or you can pop us a mail at help.looker.com

Right here while logged in to your instance as long as you are at least a Developer:


Userlevel 4
Badge

@svarun, what is this related to exactly? What Scott linked to is the needed swagger json file for generating the sdk. As mentioned in the article, you can to this with curl or wget: curl -0 https://master.dev.looker.com:19999/api/3.0/swagger.json > swagger.json.


The response you pasted in here seems to be related to something else. You’re getting a 404, which means the resource you’re looking for is simply not there. Please reach out via help.looker.com or pop on chat so we can have a look at your code.

Hello,


I’m having similar issues to scottmdavidson and I am unable to run a look as I get a 404 error. See code below:


import swagger_client as looker
import os


client_id = os.environ.get('LOOKER_CLIENT_ID')
client_secret = os.environ.get('LOOKER_SECRET_KEY')
#base_url = 'https://looker.mycompany.com:9999/api/3.0/'
base_url = 'https://looker.mycompany.com:19999/api/3.0/'


unauthenticated_client = looker.ApiClient(base_url)
unauthenticated_authApi = looker.ApiAuthApi(unauthenticated_client)


token = unauthenticated_authApi.login(client_id=client_id, client_secret=client_secret)
client = looker.ApiClient(base_url, 'Authorization', 'token ' + token.access_token)

looks = looker.LookApi(client)
looks.run_look(9669,'json')

The error I receive:


ApiException: (404)
Reason: Not Found
HTTP response headers: HTTPHeaderDict({'Server': 'nginx', 'Date': 'Thu, 29 Jun 2017 19:14:36 GMT', 'Content-Type': 'application/json;charset=utf-8', 'Content-Length': '69', 'Connection': 'keep-alive', 'Vary': 'Accept-Encoding', 'X-Content-Type-Options': 'nosniff'})
HTTP response body: {"message":"Not found","documentation_url":"http://docs.looker.com/"}

I can authenticate successfully and receive a token. I tested that access_token in curl and it retrieves my user info successfully. Any help here to run a look would be great!

Ok thanks for the advice.



  1. I can see the look in the front end (I created it).


Can you point me in the direction to run the look via curl or tell me the syntax. Below is what I have:


curl https://looker.mycompany:19999/api/3.0/looks/15133/run/csv -H "Authorization: Bearer access_token here"

Hey,


I still received the 404 error but the issue is resolved. You were correct it was a permission issue.


I re-looked (no pun intended) at the info from my original curl query and found the space id from the curl query was different than the space id from the front end application. I contacted the admin and the admin setup new api keys for the “right” account. Thanks for the help!

I am not able to get the GIT.


git clone git@github.com:swagger-api/swagger-codegen.git


It prompts me an error


Cloning into ‘swagger-codegen’…

ssh: connect to host github.com port 22: Connection refused

fatal: Could not read from remote repository.


Please make sure you have the correct access rights

Reply