2

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

RWB
  • 107
  • 8
  • What do the API docs (if any) say? – QHarr Feb 05 '22 at 00:24
  • I am a beginner, so I am not sure. I have asked the organization (USGA) via email, for the documentation and instruction, but so far nothing, I got a tremendous amount of help on the first round. The original code was built by "asmitu" – RWB Feb 05 '22 at 02:59
  • Did you follow the comment thread in [original answer](https://stackoverflow.com/a/61074520/) and monitor the web-traffic when manually logging in and seeing what is going on? – QHarr Feb 05 '22 at 03:20
  • I don't know how to do that as I am new to web scraping in general and the use of API. Thank you. I will research that method – RWB Feb 06 '22 at 10:11

3 Answers3

1

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)
Chris Jones
  • 129
  • 8
  • I just wrote an HTML Webscrape workaround. It works well, but of course much slower than JSON or XML – RWB Feb 21 '22 at 19:06
1

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'];
}
?>
John K.
  • 33
  • 4
  • Oh yeah, for sure you can just use the API while authenticated. I just don't have a login for GHIN. I get my HCP via TheGrint, so I don't have access directly to GHIN. From what I read, that will change this fall though so we'll see. – Chris Jones Feb 25 '22 at 15:07
-1

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.