我正在尝试创建一个仅限浏览器的应用,以便在Vimeo上访问我喜欢的视频 .

我按照https://developer.vimeo.com/api/authentication中的说明进行操作

当我使用url中的代码获取重定向到我的页面时,我尝试进行Ajax调用以获取身份验证令牌,但由于cors问题,它不允许我 .

我现在正在通过 localhost:8080 工作,我将它注册为我的应用程序URL并在我的Vimeo应用程序设置中作为回调

错误:

OPTIONS https://api.vimeo.com/oauth/access_token 405 (Method Not Allowed)

Failed to load https://api.vimeo.com/oauth/access_token: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 405.

我的代码:

const getAccessToken = function(code) {
    const xhr = new XMLHttpRequest()
    const url = 'https://api.vimeo.com/oauth/access_token'
    xhr.open('POST', url, true)

    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
    xhr.setRequestHeader('Authorization', 'basic ' + btoa(client_id + ':' + client_secret))

    const params = `grant_type=authorization_code&code=${code}&redirect_uri=${redirect_uri}`

    xhr.onload = function(ev) {
      console.log('response', xhr.readyState, xhr.status)
      if (this.readyState == 4 && this.status == 200) {
        alert(this.responseText)
      }
    }

    xhr.onerror = function(ev) {
      console.log('error', this.readyState, this.status, this)
    }

    xhr.send(params)
  }