堆栈溢出的老问题<https://stackoverflow.com/questions/61073325/vba-xml-web-log-in-to-usga-ghin-website-not-working>
美国地质勘探局网站管理员https://www.ghin.com/login第2页-美国地质勘探局https://www.ghin.com/golfer-lookup/following
我构建了一个ExcelVBA应用程序,该应用程序使用从USGA网站收集的数据和API/JSON数据提取,我是USGA网站的授权用户,拥有有效的帐户和密码。然而,我已经可靠地使用了大约2年的代码现在正在生成一个";无效令牌错误";。
";无效令牌错误";可能与密码有关。我以前的代码不需要输入密码。我已经厌倦了将密码输入构建到输入/响应中,但到目前为止还没有运气?
对你的任何想法都要解决";无效令牌错误";还有可能,构造我的密码输入?这是可能的旧代码(也张贴在上面的堆栈溢出链接(
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
此处相同。我的页面是用PHP编写的,所以你必须根据VBA进行相应的调整。看起来他们现在需要密码,而过去只需要姓氏和ghin号码。我将下面的POST与下面$postdata中显示的数组一起使用。但GET可能仍然有效。祝你好运
<?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/jsonn" .
"Accept: application/jsonn" .
"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'];
}
?>
我也有一个应用程序使用GHIN API。他们似乎只是删除了公共端点(https://api2.ghin.com/api/v1/public)。您现在必须进行身份验证才能获得令牌来使用他们的API。
这里有一个网站,为GHIN API提供了一些非常好的文档:https://app.swaggerhub.com/apis-docs/GHIN/Admin/1.0
很抱歉,我相信这不是你想听到的答案。
编辑:现在我有了GHIN,可以登录了。下面是我的工作Python代码:
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)
伙计们,如果你有一个活跃的GHIN,那就很容易了。
- 下载TheGrint
- 注册
- 将您的GHIN链接到您新创建的帐户
- 开始将你的分数从TheGrint发布到USGA
- 让你连夜复习障碍
就这么简单。
享受TheGrint。