首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >FeathersJS Twitch 401未经授权

FeathersJS Twitch 401未经授权
EN

Stack Overflow用户
提问于 2020-07-25 14:17:04
回答 1查看 226关注 0票数 0

我是FeathersJS的新手。我试着用Twitch设置OAuth登录。我创建了一个twitch应用程序,并对github登录进行了与这里相同的操作。我想将用户保存在我的MongoDB数据库中,但是在登录twitch之后,我将被重定向到MongoDB。我用羽毛制作了一个新的应用程序。我做错了什么?

config/default.json

代码语言:javascript
运行
复制
...
"oauth": {
  "redirect": "/",
  "twitch": {
    "key": ""*****************",",
    "secret": ""***************************************"",
    "scope": ["user:read:email"]
  },
  "github": {
    "key": "*****************",
    "secret": "***************************************"
  }
}...
EN

回答 1

Stack Overflow用户

发布于 2020-10-01 04:25:04

这是因为内置策略试图获取用户配置文件,但它没有在头中包含客户端ID (请参阅API API-入门 )。

要解决这个问题,您需要创建实现getProfile的自己的策略。我使用了从羽毛烹饪书的Facebook演示作为参考在这里可以找到

以下是我的实现:

./策略/TwitchStrategy.ts

代码语言:javascript
运行
复制
import { Params } from '@feathersjs/feathers'
import { AuthenticationRequest } from '@feathersjs/authentication'
import { OAuthStrategy, OAuthProfile } from '@feathersjs/authentication-oauth'
import axios from 'axios'
import { Application } from '../declarations'

export class TwitchStrategy extends OAuthStrategy {
    // we need a reference to the app instance
    app: Application = {} as Application

    // when the strategy is initialized this method is called with an app instance
    setApplication(appInstance: Application): void {
        this.app = appInstance
    }

    // this method is used to get the user profile after they authorize with the provider
    async getProfile(authResult: AuthenticationRequest, _params: Params) {
        const accessToken = authResult.access_token

        const { data } = await axios.get('https://api.twitch.tv/helix/users', {
            headers: {
                Authorization: `Bearer ${accessToken}`, //our users access token to look them up
                'Client-ID': this.app.get('authentication').oauth.twitch.key //we need to send the Client-ID
            },
            params: {
                fields: 'id,name,email'
            }
        })

        console.log(data)

        return data
    }

    async getEntityData(profile: OAuthProfile, existing: any, params: Params) {
        // `profile` is the data returned by getProfile
        const baseData = await super.getEntityData(profile, existing, params)

        return {
            ...baseData,
            email: profile.email
        }
    }
}

./认证

代码语言:javascript
运行
复制
import { ServiceAddons } from '@feathersjs/feathers'
import { AuthenticationService, JWTStrategy } from '@feathersjs/authentication'
import { LocalStrategy } from '@feathersjs/authentication-local'
// import our strategy
import { TwitchStrategy } from './strategies/TwitchStrategy'
import { expressOauth } from '@feathersjs/authentication-oauth'
import { Application } from './declarations'

declare module './declarations' {
    interface ServiceTypes {
        authentication: AuthenticationService & ServiceAddons<any>
    }
}

export default function (app: Application): void {
    const authentication = new AuthenticationService(app)
    authentication.register('jwt', new JWTStrategy())
    // register our custom strategy
    authentication.register('twitch', new TwitchStrategy())
    authentication.register('local', new LocalStrategy())

    app.use('/authentication', authentication)
    app.configure(expressOauth())
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63089423

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档