gpt4 book ai didi

angular - 如何以 Angular 2获取谷歌联系人

转载 作者:太空狗 更新时间:2023-10-29 18:32:03 28 4
gpt4 key购买 nike

我正在尝试在我的 Angular 2 项目中获取谷歌联系人这是我的代码,请帮助我,在此先感谢。在组件中我这样做了:

googleContacts(){
this.contactService.googleLogin()
.subscribe(
response => {
this.logger.info("addContactComponent googleContacts(): "+ response);
window.location.href = ""+ response;

},
error => this.logger.error( error ),
() => this.logger.info( "google_contacts() finished" )
)
}

在服务中我是这样做的:

googleLogin():Observable<Response> {
this.logger.info(this.googleContactsUrl+"authorizeLogin?access_token=" + this.authenticationService.access_token);
return this._http.get(this.googleContactsUrl+"authorizeLogin?access_token=" + this.authenticationService.access_token)
.map((response: any) => response);
}

在 html 中:

<button style="margin: -8px; background-color: white" (click) = "googleContacts()"></button >

最佳答案

这就是我在 Angular4 中使用它的方式。首先,您需要在 index.html 中包含 Google API 脚本:

  <script src="https://apis.google.com/js/platform.js"></script>
<script src="https://apis.google.com/js/client.js"></script>

将 gapi 类型添加到您的项目中

npm install --save @types/gapi
npm install --save @types/gapi.auth2

在你的组件中,

ngOnInit() {
this.authConfig = {
client_id: '<YOUR-CLIENT-ID>',
scope: 'https://www.googleapis.com/auth/contacts.readonly'
};
}

googleContacts() {
gapi.client.setApiKey('<YOUR-API-KEY>');
gapi.auth2.authorize(this.authConfig, this.handleAuthorization);
}

handleAuthorization = (authorizationResult) => {
if (authorizationResult && !authorizationResult.error) {
let url: string = "https://www.google.com/m8/feeds/contacts/default/thin?" +
"alt=json&max-results=500&v=3.0&access_token=" +
authorizationResult.access_token;
console.log("Authorization success, URL: ", url);
this.http.get<any>(url)
.subscribe(
response => {
if (response.feed && response.feed.entry) {
console.log(response.feed.entry);
}
}
)
}
}

不要忘记导入 gapi 类型,否则你会得到 gapi 未定义的错误;

import { } from '@types/gapi';
import { } from '@types/gapi.auth2';

此外,如果您使用的是 Chrome,您可能需要允许来自您的应用程序域的弹出窗口,否则 Google API 身份验证将无法继续。参见 this question在 stackoverflow 上。

关于angular - 如何以 Angular 2获取谷歌联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42157255/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com