Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在angular 2中获得google联系人_Angular_Typescript_Google Contacts Api - Fatal编程技术网

如何在angular 2中获得google联系人

如何在angular 2中获得google联系人,angular,typescript,google-contacts-api,Angular,Typescript,Google Contacts Api,我试图在angular 2项目中获得google联系人 这是我的代码,请帮助我,提前谢谢。 在组件中,我这样做: googleContacts(){ this.contactService.googleLogin() .subscribe( response => { this.logger.info("addContactComponent googleContacts(): "+ r

我试图在angular 2项目中获得google联系人 这是我的代码,请帮助我,提前谢谢。 在组件中,我这样做:

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);
    }
googleLogin():可观察{
this.logger.info(this.googleContactsUrl+“authorizeLogin?access_token=“+this.authenticationService.access_token”);
返回此。_http.get(this.googleContactsUrl+“authorizeLogin?access_token=“+this.authenticationService.access_token)
.map((响应:任意)=>响应);
}
和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>
在您的组件中

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);
          }
        }
      )
  }
}

此外,如果您使用的是Chrome,您可能需要允许应用程序域中的弹出窗口,否则Google API身份验证将无法继续。有关上述解决方案,请参阅stackoverflow.

ngOnInit(){}
中包含的服务
authConfig
包含哪个包

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);
          }
        }
      )
  }
}
import { } from '@types/gapi';
import { } from '@types/gapi.auth2';