如何自动连接到 google 帐户

how to connect to google account automatically

我正在使用 angular 编写一个网站,并将在仪表板中显示来自 google 分析的统计数据和来自 google 日历的日历。 我想 link 我的网站到我的 Google 帐户,有理由不每次都连接到我的 Google 帐户所以有什么解决方案可以在后端执行 auth2 而无需每次插入时间电子邮件和密码

加载 auth2 后,我尝试通过在它后面和选项中编写登录功能来登录,我通过编写我想连接到它的电子邮件来指定登录命中

export class GoogleService {

  
private user = new ReplaySubject<gapi.auth2.GoogleUser>(1);
  

constructor(
  private analytics : AnalyticsService, 
  private calendar : CalendarService
 ) { 
 gapi.load('auth2',()=>{
  gapi.auth2.init({
    client_id : '<client-Id>'
  }).then(res=>{

    if(res.isSignedIn.get()){
      //if the user already sign in 
      this.user.next(res.currentUser.get());
      this.analytics.getReportes();
      this.calendar.getEventsApi();
      
    }else{
      
      res.signIn({
        scope : 
          // See, edit, share, and permanently delete all the calendars you can access using Google Calendar
          'https://www.googleapis.com/auth/calendar '+
    
          // View and edit events on all your calendars
          'https://www.googleapis.com/auth/calendar.events '+
    
          // View events on all your calendars
          'https://www.googleapis.com/auth/calendar.events.readonly '+
    
          // See and download any calendar you can access using your Google Calendar
          'https://www.googleapis.com/auth/calendar.readonly '+
    
          // View your Calendar settings
          'https://www.googleapis.com/auth/calendar.settings.readonly '+

          // View analytics reports 
          'https://www.googleapis.com/auth/analytics.readonly' ,

        //account to connect
          login_hint:'<account-email>'
  
      }).then(user=>{
        this.user.next(user);
        this.analytics.getReportes();
        this.calendar.getEventsApi();
      }).catch(()=>{
        this.user.next(null)
      })
    }

  })
 })
}



public observable() : Observable<gapi.auth2.GoogleUser>{
  return this.user.asObservable();
}

  
}

第一次会要求输入密码,下次登录注册密码后会自动连接。

这对我有用!