首页 > 解决方案 > 使用 REST 端点获取 Youtube Data API v3 OAuth2 令牌时出现问题

问题描述

我一直在尝试使用以下代码在本机反应中获取 YouTube OAuth2 令牌:

let url =
  'https://accounts.google.com/o/oauth2/v2/auth?' +
  new URLSearchParams({
    client_id: encodeURIComponent(clientId),
    redirect_uri: encodeURIComponent('http://localhost:19006/oauth2callback'),
    scope: encodeURIComponent('https://www.googleapis.com/auth/youtube'),
    response_type: 'token'
  });

//Fetch the URL
fetch(url)
  .then(function (response) {
    // Log and return the response as JSON
    let rJson = response.json();
    console.log(rJson);
    return rJson;
  })
  .then(function (data) {
    // Log the API data
    console.log('token', data);
  })
  .catch(function (err) {
    // Log any errors
    console.log('something went wrong', err);
  });

我收到错误消息:

SyntaxError: JSON Parse error: Unrecognized token '<'

即使在查看YouTube Data API Client-side Web Apps Docs之后,我也一直坚持这一点,毫无疑问,这很简单,我看不到,但我们将不胜感激

标签: javascriptreact-nativerestyoutube-data-api

解决方案


推荐阅读