Get facebook data with ionic 2

0

I'm doing login with facebook with cordova-plugin-facebook4, just like it's on the home page link . Just give me the facebook id, I'd like to bring the email and the name.

I did it this way

config.xml

<plugin name="cordova-plugin-facebook4" spec="^1.9.1">
    <variable name="APP_ID" value="APP_id" />
    <variable name="APP_NAME" value="miproyecto" />
</plugin>

app.module.ts

import { Pages } from '../pages/pages';
import { Facebook } from '@ionic-native/facebook';

@NgModule({
 declarations: [
  pages
  .....
],
imports: [
  IonicModule.forRoot(MyApp,config, ['ngAnimate'])
],
bootstrap: [IonicApp],
entryComponents: [
 ......
],
providers: [
Notifying,
 {provide: ErrorHandler, useClass: IonicErrorHandler},
 ....
 Facebook
]
})
export class AppModule {}

login.ts

import { Facebook, FacebookLoginResponse } from '@ionic-native/facebook';


 export class Login {
   constructor(
    private fb: Facebook
   ) {}

   loginFace(ev?: Event){
    this.fb.login(['public_profile', 'user_friends', 'email'])
     .then((res: FacebookLoginResponse) => {
      console.log('Logged into Facebook!', res)

        let idFacebook = res.authResponse.userID;

        this.fb.api(res.authResponse.userID+"/?fields=id,email,first_name,last_name", [])
          .then(re => console.log(res))
          .catch(e => console.log(e));
     })
     .catch(e => {
       console.log('Error logging into Facebook', e)
     });
   }
 }

console

    
asked by Albert Arias 25.09.2017 в 17:35
source

2 answers

2

Where it says

  .then(re => console.log(res))

Replace it with

  .then(re => console.log(re))

You are seeing the result of the first request.

    
answered by 04.10.2017 / 03:15
source
1

I understand from the facebook api that when you get a login you will only return an access key and that with it you get an authorization json to the fields you require, that if your app previously requested those fields in the application Log in to facebook and the solution is to link {userId}? fields = id, email, name & access_token = {access-token}

    
answered by 27.09.2017 в 22:12