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.
Generating Clients/SDKs Using Swagger Codegen
Hey
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:
- 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} - 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$
Hey there
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:
- 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.
- 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.
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?
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
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 🙂
Could you come on chat and we’ll work on this issue with you
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.
- 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
Enter your username or e-mail address. We'll send you an e-mail with instructions to reset your password.