USGA Website - API and JSON - quit working - getting "error Invalid token"

Viewed 294

Old Question on Stack Overflow< https://stackoverflow.com/questions/61073325/vba-xml-web-log-in-to-usga-ghin-website-not-working>

USGA Webiste https://www.ghin.com/login Page 2 - USGA https://www.ghin.com/golfer-lookup/following

I built an Excel VBA app that uses the data collected from and API/JSON data-pull from the USGA website, of which I am an authorized USER with a valid account and password. However, the code which I have used reliably for about 2 years is now generating a "Invalid Token Error".

The "Invalid Token Error" may be password related. My prior code required no password input. I have tired to build the password input into the input/response but as of yet no luck?

Any thoughts on you to solve "Invalid Token Error" and possibly, construct the password input on my part? Here is may old code (Also posted on the Stack Overflow links above)

        
Sub GetInformation()
    Const Url = "https://api2.ghin.com/api/v1/public/login.json?"
    Dim Http As New XMLHTTP60, ghinNum$, lastName$

    ghinNum = ""            'put your ghinNum here
    lastName = ""           'put your lastName here

    With Http
        .Open "GET", Url & "ghinNumber=" & ghinNum & "&lastName=" & lastName & "&remember_me=false", False
        .setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
        .setRequestHeader "Referer", "https://www.ghin.com/login"
        .send
    End With

    MsgBox Http.responseText
End Sub

3 Answers

I too have an app that used the GHIN API as well. It appears they simply removed their public endpoint (https://api2.ghin.com/api/v1/public). You now have to authenticate to get a token to use their API.

Here's a site with some really good documentation for the GHIN API: https://app.swaggerhub.com/apis-docs/GHIN/Admin/1.0

Sorry that's not the answer you wanted to hear, I'm sure.

EDIT: Now I have a GHIN and can log in. Here's my working Python code:

import requests
import json

ghinNum = "GHINID"
password = "GHINPASS"

headers = {
    "Content-Type": "application/json; charset=utf-8",
    "Accept": "application/json",
    }

data = {
    "user": {
        "email_or_ghin": ghinNum,
        "password": password,
        "remember_me": "true",
    },
    "token": "nonblank",
}

r = requests.post("https://api2.ghin.com/api/v1/golfer_login.json", headers=headers, json=data)

headers["Authorization"] = "Bearer " + r.json()["golfer_user"]["golfer_user_token"]

url = "https://api.ghin.com/api/v1/golfers/search.json?per_page=1&page=1&golfer_id=" + ghinNum

r = requests.get(url, headers=headers)

if r.status_code == 200:
    data = r.json()
    handicap_index = data['golfers'][0]['handicap_index']

    print(handicap_index)

else:
    print(r.status_code)

Same here. My page is in PHP, so you will have to adjust accordingly to VBA. It looks like they are now requiring the password whereas it used to work with just the last name and ghin number. I use POST below with the array I show in the $postdata below. But GET may still work. Good luck.

<?PHP
// my new PHP login code
$ghin = $my_info['ghin']; // 7-digit number
$pw = $my_info['ghinpw'];
    
$postdata = json_encode(
    array(
        'user' => array(
            'email_or_ghin' => $ghin,
            'password' => $pw,
            'remember_me' => 'true',
        ),
        'token' => 'nonblank'
    )
);
// gave me warning about wanting a nonblank token, so I gave it one!

$options = array('http' =>
    array(
        'header' => 
            "Content-type: application/json\n" .
            "Accept: application/json\n" .
            "user_agent: Mozilla/5.0 (X11; CrOS x86_64 14469.16.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.100.4844.23 Safari/537.36",
        'method' => 'POST',
        'content' => $postdata
    )
);
$context = stream_context_create($options);

$url = "https://api2.ghin.com/api/v1/golfer_login.json";
$contents = file_get_contents($url, false, $context);

$results = json_decode($contents, true);
$token = $results['golfer_user']['golfer_user_token'];

$options = array('http' => array(
    'method' => 'GET',
    'header' => 'Authorization: Bearer ' . $token
));

$context = stream_context_create($options);

// then you can grab URLs
$url = "https://api.ghin.com/api/v1/golfers/$ghin/handicap_history_count.json?rev_count=3";
$contents = file_get_contents($url, false, $context);

if ($contents) {
    $results = json_decode($contents, true);
    $handi = $results['handicap_revisions'][0]['Value'];
}
?>

Guys In you have an Active GHIN then it's easy.

  1. Download TheGrint.
  2. Register.
  3. Link your GHIN to your newly created account.
  4. Start posting your scores from TheGrint to the USGA
  5. Get you handicap revise overnight.

As simple as that.

Enjoy TheGrint.

Related