我正在使用 ReactNative 开发 Twitter 身份验证。 我正在使用“expo-auth-session”库。 使用以下代码执行“tokenRequest.performAsync(discovery)”时,会发生以下错误。 是什么原因?
语法错误:JSON 解析错误:意外字符:<
import {makeRedirectUri, useAuthRequest, AccessTokenRequest} from 'expo-auth-session';
...
const discovery = {
authorizationEndpoint: "https://twitter.com/i/oauth2/authorize",
tokenEndpoint: "https://twitter.com/2/oauth2/token",
revocationEndpoint: "https://twitter.com/i/oauth2/revoke",
};
const reqConfig = {
clientId: TWITTER_CLIENT_ID,
clientSecret: TWITTER_CLIENT_SECRET,
redirectUri: makeRedirectUri({
scheme: 'jp.pazle.voice',
path: 'twitter/callback',
}),
usePKCE: true,
scopes: [
'tweet.read',
'tweet.write',
'offline.access',
],
};
const [request, response, promptAsync] = useAuthRequest(
{...reqConfig},
discovery,
);
useEffect(() => {
if (response?.type === 'success') {
const {code, state} = response.params;
const tokenRequest = new AccessTokenRequest({
...reqConfig,
code,
extraParams: {
code_verifier: request.codeVerifier,
state,
},
});
(async() => {
try {
const tokenResult = await tokenRequest.performAsync(discovery);
} catch (e) {
console.log(e);
}
})();
}
}, [response]);
return (
<View>
<Button onPress={() => promptAsync} title={'login'}></Button>
</View>
);
仅基于
JSON Parse error: Unexpected character: <
,看起来 HTML 被解析为 JSON。发生这种情况时,请仔细检查任何 HTTP 端点并确保正在传递 Content-Type
标头。
看看是否可以将
response
对象转储到日志中,并在调用 tokenRequest
之前查看一下 performAsync
中定义的内容。
这看起来相关:无法使用 Expo 与 Twitter 进行身份验证