【问题标题】:React Native import Facebook pictureReact Native 导入 Facebook 图片
【发布时间】:2019-04-20 02:31:48
【问题描述】:

如何获取用户ID邮件图片等?图片非常重要。我已经尝试了不同的示例,但它们没有用。我想在第二个屏幕上显示图片,但目前这并不重要。抱歉,我是新手,不知道该怎么做。

  import React from 'react';
  import {
    StyleSheet,
    Text,
    View,
    Button,
    Alert,
    TouchableOpacity,
    TextInput,
    Switch,
    Image
  } from 'react-native';
  import EStyleSheet from 'react-native-extended-stylesheet';
  import { LoginManager,LoginButton,AccessToken,GraphRequest,GraphRequestManager} from 'react-native-fbsdk';
  import { createStackNavigator } from 'react-navigation';
  import * as firebase from 'firebase';


  EStyleSheet.build({
      $textColor: 'white'
    });


  export default class HomeScreen extends React.Component {
      constructor(props){
          super(props);
          this.state = {userInfo: null,};
      }
      static navigationOptions = {
          title: "Socialmedia", 
      };
      renderImage(){

            console.log(this.state.userInfo);
            this.render(
              <View>
                <Image source={{url: this.state.userInfo.photoURL}} />
              </View>
            )
      }
      //Facebook login
      async loginWithFacebook() {
    try {
      const {
        type,
        token,
        expires,
        permissions,
        declinedPermissions,
      } = await Expo.Facebook.logInWithReadPermissionsAsync('262997934355158', {
        permissions: ['public_profile'],
      });
      if (type === 'success') {
        const credential = firebase.auth.FacebookAuthProvider.credential(token)
        //this.props.navigation.navigate('Second', {});
        firebase.auth().signInAndRetrieveDataWithCredential(credential).catch((error) => {
          console.log(error)
        })
        // Get the user's name using Facebook's Graph API
        const response = await fetch(`https://graph.facebook.com/me?access_token=${token}&fields=id,name,picture,type(large)`);
        const userInfo = await response.json();
        this.setState({userInfo});
        this.renderImage();
      } else {
        // type === 'cancel'
      }
    } catch ({ message }) {
      alert(`Facebook Login Error: ${message}`);
    }
  }
  //Google login
        async loginWithGoogle() {
          const { type, token } = await await Expo.Google.logInAsync({
              //androidClientId: YOUR_CLIENT_ID_HERE,
              iosClientId: '258088831368-h42kgsqarctlpuaugksgqeh6plkh0ese.apps.googleusercontent.com',
              scopes: ['profile', 'email'],
            });
          if (type == 'success') {
            const credential = firebase.auth.GoogleAuthProvider.credential(token)
            firebase.auth().signInAndRetrieveDataWithCredential(credential).catch((error) => {
              console.log(error)
            })
          }
        }
        //339771303079-3mse4b0pvur499bof3ri61heahjtsj9f.apps.googleusercontent.com
      render() {
          return(
          <View style={styles.container}>
              <View style={styles.container}><TouchableOpacity style={[styles.buttons, {backgroundColor: '#3b5998'}]} onPress={this.loginWithFacebook.bind(this)}>
              <Image style={styles.img} source={require("../assets/Facebook.png")}/><Text style={{left: 20, color: 'white'}}>Facebook</Text>
              </TouchableOpacity></View>
              <View style={styles.container}><TouchableOpacity style={styles.buttons} onPress={() => this.loginWithGoogle()}>
              <Image style={styles.img} source={require("../assets/Google.png")}/><Text style={{left: 20}}>Login with Google</Text>
              </TouchableOpacity></View>
              <View style={styles.container}><TouchableOpacity style={styles.buttons}>
              <Text>Github</Text>
              </TouchableOpacity></View>
              <View style={styles.container}><TouchableOpacity style={styles.buttons}>
              <Text>Play Spiele</Text>
              </TouchableOpacity></View>
              <View style={styles.container}><TouchableOpacity style={styles.buttons}>
              <Text>Twitter</Text>
              </TouchableOpacity></View>
          </View>
          );
      }
  }
  })

这是调试信息: {错误: {…}} 错误 : {消息:“语法错误”预期字符串结尾而不是字符 20 处的“(”。“:id,名称,图片,类型(大)”,类型:“OAuthException”,代码:2500,fbtrace_id:“DXFuk9C8gdT”} 原型 : 对象

【问题讨论】:

  • 您能给我们提供的不仅仅是“它不起作用”吗?调试您的代码并告诉我们有关响应/错误/...btw,这没有任何意义:const userInfo = await fetch();
  • 我更改了代码并添加了错误消息这只是一个错误我还修复了'const userInfo = await fetch();'我是新手,所以真的不知道该怎么办我很抱歉@luschn
  • 我修复了图片的问题 问题是这样的:https://graph.facebook.com/me?access_token=${token}&amp;fields=id,name,picture,type(large) 应该是:https://graph.facebook.com/me?access_token=${token}&amp;fields=id,name,picture.type(large)

标签: android ios facebook react-native login


【解决方案1】:

语法错误告诉你API调用有问题:

错误

picture,type(large)

正确

picture.type(large)

顺便说一句,您可以(并且应该)总是先在 API Explorer 中尝试 API 调用:https://developers.facebook.com/tools/explorer/

【讨论】:

  • 感谢工作,我通过导航将 url 转移到了另一个类。
【解决方案2】:

您不需要额外的请求。

只需这样做。

firebase.auth().signInAndRetrieveDataWithCredential(credential)
.then(data => {
  let {photoURL} = data.user
  // now you got the photoURL
  // to retrieve different sizes of photo url simply do
  let largerImage = photoURL+"?height=600"
})

一个简单的请求。

height=600 将根据原始照片的宽度和高度属性调整照片大小,并返回适当大小的图像。它将返回 600 以上,但不会低于。因为图片可能不是正方形格式。

【讨论】:

  • 我还没有安装 SDK,所以这不起作用 dir ne nur thx
  • 你不需要安装sdk。 firebase.auth().signInAndRetrieveDataWithCredential(credential) 您在提供的示例中有此代码。
  • 这行得通,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-24
  • 2020-04-27
  • 2021-04-20
  • 1970-01-01
  • 2018-06-08
  • 2017-06-29
相关资源
最近更新 更多