A JOSSO_SESSIONID is an easy way to access any ScienceBase items that you have access to RESTfully without having to log in to make each request. Once you have obtained a JOSSO_SESSIONID you can use this as your login.

For more detailed information see Authenticating

Login Example

In this example we use HTTPComponents to log in and grab our JOSSO_SESSIONID from the correct cookie.

package gov.usgs

@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.2.1')
@Grab(group='org.apache.httpcomponents', module='httpmime', version='4.2.1')

import org.apache.http.client.*
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.client.methods.*
import org.apache.http.util.EntityUtils

//ScienceBase URL
def baseUrl = ""
//ScienceBase beta URL
//def baseUrl = ""

//Username and password, these should be a service account. Usernames must be all lowercase, check logging in to
// with the exact username and password if logging in fails.
def username = "sbdocumentationtesting"
def password = "..."

String josso_sessionid = login(baseUrl, username, password)

//Check that we logged in correctly
if(!josso_sessionid) {println "fail"; System.exit(1)}
println josso_sessionid

 * Login to ScienceBase and return a HttpClient
 * @param baseUrl   Base URL to authenticate on, eg.
 * @param username  The plain text username to authenticate on, use your own or a service account
 * @param password  The plain text password to authenticate on
 * @return          The josso_sessionid from the cookie received when logging in
String login(String baseUrl, String username, String password){
    def encodedUsername = URLEncoder.encode(username)
    def encodedPassword = URLEncoder.encode(password)

    //Create a client for sending files, they will share cookies through this
    HttpClient httpClientLogin = new DefaultHttpClient()
    HttpGet httpGet = new HttpGet(("${baseUrl}/?josso_username=${encodedUsername}&josso_password=${encodedPassword}").toURI())
    httpGet.addHeader("Accept", "application/json")
    def loginResponse = httpClientLogin.execute(httpGet)
    //Must do this to complete the login and release httpClientLogin
    def loginString = EntityUtils.toString(loginResponse.getEntity())

    def josso_sessionid_cookie = httpClientLogin.getCookieStore().getCookies().find{ == "JOSSO_SESSIONID"}
    if(loginResponse.statusLine.toString().contains("200") && josso_sessionid_cookie){
        return josso_sessionid_cookie.value
    else return null