Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do you access a jenkins api which uses Github OAuth using CURL

I have a jenkins server using the Github OAuth plugin and authorized in the "Authorized applications" section of github, it works fine from my browser, i can access to the jenkins server as long as i'm authenticated with github.

Is there a way to access to the jenkins server api using oauth credentials/token from CURL or a ruby client?

I've generated a token in https://github.com/settings/applications -> Personal access tokens -> Generate new token (there is no option to scope it to a third party application)

that token works fine to access github :

curl -H "Authorization: token cfbcff42e6a8a52a1076dd9fcxxxxxxxxxxxxxxx" https://api.github.com/user

however, that token is not valid for jenkins-server:

curl -H "Authorization: token cfbcff42e6a8a52a1076dd9fcxxxxxxxxxxxxxxx" https://jenkins-server/user/restebanez/api/json/\?pretty\=true

It generates this error:

<html><head><meta http-equiv='refresh' content='1;url=/securityRealm/commenceLogin?from=%2Fuser%2Frestebanez%2Fapi%2Fjson%2F%3Fpretty%3Dtrue'/><script>window.location.replace('/securityRealm/commenceLogin?from=%2Fuser%2Frestebanez%2Fapi%2Fjson%2F%3Fpretty%3Dtrue');</script></head><body style='background-color:white; color:white;'>


    Authentication required
<!--
You are authenticated as: anonymous
Groups that you are in:

                        Permission you need to have (but didn't): hudson.model.Hudson.Read
 ... which is implied by: hudson.security.Permission.GenericRead
 ... which is implied by: hudson.model.Hudson.Administer
-->

</body></html>     

```

the jenkins server has installed GitHub API Plugin 1.58 and Github Authentication plugin 0.19

I'm probably missing some fundamentals of oauth b/c i have googled this for a while and i haven't found anything

like image 807
Rodrigo Estebanez Avatar asked Sep 04 '14 20:09

Rodrigo Estebanez


People also ask

How do I pass Jenkins credentials in curl command?

With Jenkins CLI you do not have to reload everything - you just can load the job (update-job command). You can't use tokens with CLI, AFAIK - you have to use password or password file. Token name for user can be obtained via http://<jenkins-server>/user/<username>/configure - push on 'Show API token' button.

How do I authenticate GitHub from Jenkins?

The GitHub Authentication Plugin provides a security realm to authenticate Jenkins users via GitHub OAuth. In the Global Security configuration choose the Security Realm to be GitHub Authentication Plugin. The settings to configure are: GitHub Web URI, GitHub API URI, Client ID, Client Secret, and OAuth Scope(s).


1 Answers

I'm not sure if you ever got to the bottom of this, but after trying several routes I finally got a scripted build using Github OAuth on Jenkins. The trick is that the API token is not one for GitHub but rather one from Jenkins.

For my setup I have a machine user on github, I logged in normally via the web with that user, then clicked on the username in the upper right corner. From there I clicked "Configure" on the left-hand menu, and finally "Show API Token" in the main content area.

Once I had that I could run:

curl --user <username>:<api_token> https://jenkins-server/user/<username>/api/json/?pretty=true

More information.

like image 185
mjschultz Avatar answered Sep 18 '22 07:09

mjschultz