Need some pointers with oauth 2.0 and sha256

anybody have an example of some app code that does something like the following

thanks in advance

Authentication - Tesla JSON API (Unofficial) (timdorr.com)

so far gave up on this for now unless some can figure out (other thread) why the first call to the api is not returning the correct results even though i get a 200 back?
Instead for now I made some changes to run a .py script on a server and push new token to my local qnap web server and refresh token from that when needed. I did solve the sha256 and base64 encoding which in itself took me a half a day. ie.. Here is the code for that if it helps anyone else.

import java.security.MessageDigest

String.metaClass.encodeURL = {
java.net.URLEncoder.encode(delegate, "UTF-8")
}

def giveMeKey(int length)
{
String alphabet = (('A'..'N')+('P'..'Z')+('a'..'k')+('m'..'z')+('2'..'9')).join()
key = new Random().with {
(1..length).collect { alphabet[ nextInt( alphabet.length() ) ] }.join()
}
return key
}

public static String sha256(String base) {
try{
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(base.getBytes("UTF-8"));
StringBuffer hexString = new StringBuffer();

    for (int i = 0; i < hash.length; i++) {
        String hex = Integer.toHexString(0xff & hash[i]);
        if(hex.length() == 1) hexString.append('0');
        hexString.append(hex);
    }

    return hexString.toString();
} catch(Exception ex){
   throw new RuntimeException(ex);
}

}

log.debug "in Get token from login"
def verifier = giveMeKey(86).toString()

if (!state.internalState)
{
def internalState = UUID.randomUUID().toString()
state.internalState = internalState
log.info "Set internal state to $state.internalState"
}
else
{
log.info "Internal State = $state.internalState"
}

log.info " verifier = $verifier"

String challenge = sha256(verifier)
def b64Challenge = challenge.encodeAsBase64()

log.debug "before strip b4challenge = $b64Challenge"

int loc = b64Challenge.indexOf("=")
if (loc > 0)
{
   log.debug "found loc = $loc stripping off ="
   b64Challenge = b64Challenge.substring(0,loc-1)
}
1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.