该问题来自社区帖: https://bbs.csdn.net/topics/616832552.为符合问答规范, 该问题经过ChatGPT优化
以下是优化后的问题: ``` ISAPI模式下的JOSN获取异常,Standalone下又是正常的 这是调用代码:
以下是优化后的问题: ``` ISAPI模式下的JOSN获取异常,Standalone下又是正常的 这是调用代码:
procedure TMainForm.UniFormShow(Sender: TObject);
var
PathInfo, access_token: string;
GetJSON: TGetJSON;
RequestJSON, RequestJSONS: TJSONObject;
begin
PathInfo := UniApplication.Parameters.Values['authCode'];
if PathInfo = '' then
begin
UniSession.AddJS('window.location.href = "https://login.dingtalk.com/oauth2/challenge.htm?redirect_uri=http://175.16.27.83:8023&response_type=code&client_id=ding0&scope=openid&state=dddd&prompt=consent";');
exit;
end;
RequestJSON := TJSONObject.Create;
RequestJSONS := TJSONObject.Create;
RequestJSON := GetJSON.GetUsertoken(PathInfo);
try
access_token := RequestJSON.GetValue('accessToken').Value;
except
UniSession.AddJS('window.location.href = "https://login.dingtalk.com/oauth2/challenge.htm?redirect_uri=http://175.16.27.83:8023&response_type=code&client_id=ding0w5x6uud3otz1sfh&scope=openid&state=dddd&prompt=consent";');
exit;
end;
RequestJSONS := GetJSON.GetLoginUser(access_token);
if RequestJSONS.GetValue('avatarUrl').Value <> '' then
begin
UniImage3.Url := RequestJSONS.GetValue('avatarUrl').Value;
end;
UniLabel1.Caption := RequestJSONS.GetValue('nick').Value;
UniMenuItemAdd();
end;
这是实现代码:
function TGetJSON.GetUsertoken(Auto_Code: String):TJSONObject;
var
RequestJSON, ResponseJSON: TJSONObject;
ResponseText, vurl, JsonString: string;
js: TStringStream;
begin
RequestJSON := TJSONObject.Create;
ResponseJSON := TJSONObject.Create;
vurl := 'https://api.dingtalk.com/v1.0/oauth2/userAccessToken';
try
// 构建要发送的 JSON 数据
RequestJSON.AddPair(TJSONPair.Create('clientId', 'dingh'));
RequestJSON.AddPair(TJSONPair.Create('clientSecret', 'xHdgoZxqVWSp'));
RequestJSON.AddPair(TJSONPair.Create('code', Auto_Code));
RequestJSON.AddPair(TJSONPair.Create('refreshToken', Auto_Code));
RequestJSON.AddPair(TJSONPair.Create('grantType', 'authorization_code'));
JsonString := RequestJSON.ToString;
js := TStringStream.Create(JsonString);
// 设置请求的 Content-Type 为 application/json
MainForm.IdHTTP1.Request.ContentType := 'application/json';
// 发送 POST 请求,获取响应
ResponseText := MainForm.IdHTTP1.Post(vurl, js);
// 解析返回的 JSON 数据
ResponseJSON := TJSONObject.ParseJSONValue(ResponseText) as TJSONObject;
Result := ResponseJSON;
except
Result := nil;
end;
end;
经过测试在一个获取JSON数据的时候报错, 在RequestJSON.GetValue('accessToken').Value;获取信息的时候提示异常。在ISAPI里面需要调整那些地方。
```