【发布时间】:2021-10-05 00:09:23
【问题描述】:
我正在用 Angular 编写一个网站,并将在仪表板中显示来自谷歌分析的统计数据和来自谷歌日历的日历。 我想将我的网站链接到我的 Google 帐户,理由是每次都不连接到我的 Google 帐户,所以有没有任何解决方案可以在后端执行 auth2 而无需每次都插入电子邮件和密码
【问题讨论】:
标签: angular google-api google-authentication
我正在用 Angular 编写一个网站,并将在仪表板中显示来自谷歌分析的统计数据和来自谷歌日历的日历。 我想将我的网站链接到我的 Google 帐户,理由是每次都不连接到我的 Google 帐户,所以有没有任何解决方案可以在后端执行 auth2 而无需每次都插入电子邮件和密码
【问题讨论】:
标签: angular google-api google-authentication
加载 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();
}
}
第一次会要求输入密码,下次登录并注册密码后会自动连接。
这对我有用!
【讨论】: