visitorOauth method
Implementation
Future<OAuth> visitorOauth(String? username, String? password) async {
//
var oauthUrl = BytedeskUtils.getHostUri('/visitor/token');
Map<String, String> headers = {
"Authorization": "Basic Y2xpZW50OnNlY3JldA=="
};
Map<String, String> bodyMap = {
"username": "$username",
"password": "$password",
"grant_type": "password",
"scope": "all"
};
final oauthResponse =
await httpClient.post(oauthUrl, headers: headers, body: bodyMap);
// debugPrint('oauth result: $oauthResponse');
int statusCode = oauthResponse.statusCode;
// 200: 授权成功,否则授权失败
final oauthJson = jsonDecode(oauthResponse.body);
debugPrint('statusCode:$statusCode, oauth:$oauthJson');
if (statusCode == 200) {
SpUtil.putBool(BytedeskConstants.isLogin, true);
SpUtil.putString(
BytedeskConstants.accessToken, oauthJson['access_token']);
} else if (statusCode == 400) {
// token过期 {error: invalid_grant, error_description: Bad credentials}
bytedeskEventBus.fire(InvalidTokenEventBus());
}
//
return OAuth.fromJson(statusCode, oauthJson);
}