首页 > 解决方案 > Vue-Authenticate for Github Login

问题描述

我使用Vue-Authenticate插件进行社交登录,这是我的 Github 登录配置

 providers: {
    github: {
      clientId: 'my Id',
      redirectUri: 'https://' + process.env.PROJECT_DOMAIN,
      responseType: 'token',
      authorizationEndpoint: 'https://github.com/login/oauth/authorize',
    },
  },

而在认证后调用方法的方法是

 authenticate(provider) {
      const this_ = this
      this.$auth.authenticate(provider).then(function () {
        const token = this_.$auth.getToken()
        if (provider === 'github') {
          const options = {
            headers: {
              Authorization: 'token ' + token,
            },
          }
          this_.$http
            .post('https://api.github.com/user', options)
            .then(function (response) {
              if (response.status === 200) {
                const { email, name, picture } = response.data
                const data = {
                  email,
                  name,
                  image: picture.data.url,
                }
                this_.createOAuth2User(data, provider)
              }
            })
            .catch((error) => {
              console.log(error)
            })
        }
      })
    },

成功验证后,我能够收到访问令牌,但是当我尝试使用该令牌访问用户详细信息并点击https://api.github.com/userAPI 时,我收到 401 错误。那么在使用 github 进行身份验证时我缺少什么吗?

标签: vue.jsgithubnuxt.jsvue-authenticate

解决方案


推荐阅读