无法从 json 响应获取访问令牌

我正在我的网站上实施 Strava。我是 JavaScript 新手,我不明白我这样做是否正确。我的问题是我无法从 JSON 响应获取访问令牌。我不知道为什么。请帮我。

我得到的反应

http://img4.mukewang.com/64b0b1e00001171806550330.jpg

代码


<script>

    const auth_link = "https://www.strava.com/oauth/token"

function getActivites(res){

console.log('response2',res.json());

    const activities_link = `https://www.strava.com/api/v3/athlete/activities?access_token=${res.access_token}`

    fetch(activities_link)

        .then((res) => console.log(res.json()))

}

function reAuthorize(){

    fetch(auth_link,{

        method: 'post',

        headers: {

            'Accept': 'application/json, text/plain, */*',

            'Content-Type': 'application/json'

        },

        body: JSON.stringify({

            client_id: 'xxxx',

            client_secret: 'xxxxx',

            refresh_token: 'xxxxx',

            grant_type: 'refresh_token'

        })

    })

    .then(res => {

        var response = res.json();

        var access_token = response.access_token;

        console.log('response',access_token)

       // getActivites(res)

        })

}

reAuthorize()


</script>

我得到的访问令牌为undefined


MMMHUHU
浏览 111回答 1
1回答

千万里不及你

function reAuthorize() {&nbsp; fetch(auth_link, {&nbsp; &nbsp; method: "post",&nbsp; &nbsp; headers: {&nbsp; &nbsp; &nbsp; Accept: "application/json, text/plain, */*",&nbsp; &nbsp; &nbsp; "Content-Type": "application/json"&nbsp; &nbsp; },&nbsp; &nbsp; body: JSON.stringify({&nbsp; &nbsp; &nbsp; client_id: "55077",&nbsp; &nbsp; &nbsp; client_secret: "801a8541e8ae55cffee584a48c5bb6a233dea9a0",&nbsp; &nbsp; &nbsp; refresh_token: "08342cdf3250da9e0d1b53303fa40faf0d05965d",&nbsp; &nbsp; &nbsp; grant_type: "refresh_token"&nbsp; &nbsp; })&nbsp; }).then(response => response.json())&nbsp; &nbsp; .then(data => console.log(data));}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript