通过Instagram基本显示API + NestJS + Passport策略进行身份验证

时间:2020-10-19 11:05:16

标签: node.js authentication passport.js nestjs instagram-api

我在通过Instagram进行身份验证时遇到问题。我使用护照-Instagram策略。其他策略,例如facebook,google等也可以正常工作。但是Instagram显示了身份验证窗口,当我登录时出现错误: InternalOAuthError:无法获取用户个人资料。如何通过Instagram进行身份验证?

instagram.straegy.ts

import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Profile, Strategy } from 'passport-instagram';
import { ConfigService } from '@nestjs/config';

import { INSTAGRAM_APP_ID, INSTAGRAM_APP_SECRET } from '../constants';
import { SocialAuthDto } from '../dto/social-auth.dto';

@Injectable()
export class InstagramStrategy extends PassportStrategy(Strategy, 'instagram') {
  constructor(private readonly configService: ConfigService) {
    super({
      clientID: configService.get(INSTAGRAM_APP_ID),
      clientSecret: configService.get(INSTAGRAM_APP_SECRET),
      callbackURL: 'https://99e175f18e00.ngrok.io/auth/instagram',
      scope: ['user_profile', 'user_media'],
    });
  }

  async validate(
    accessToken: string,
    refreshToken: string,
    { name, emails, id: socialId, photos }: Profile,
    done: (err: any, user: any, info?: any) => void
  ): Promise<any> {
    const user: SocialAuthDto = {
      socialId,
      email: emails[0].value,
      firstName: name.givenName,
      lastName: name.familyName,
      avatar: photos[0].value,
    };

    done(null, user);
  }
}

0 个答案:

没有答案