- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建需要在许多地方发出 Facebook Graph API 请求的应用程序。但我不知道如何检索访问 token ,然后发出 Graph API 请求。
我正在使用 Expo、React Native 和 Firebase。我想在不安装 Xcode 和/或 Android Studio 的情况下执行此操作。
登录正常。我的代码如下:
async loginWithFacebook() {
try {
const {
type,
token,
expires,
permissions,
declinedPermissions,
} = await Expo.Facebook.logInWithReadPermissionsAsync('<APP_ID', {
permissions: ['email', 'public_profile'],
});
if (type === 'success') {
const credential = f.auth.FacebookAuthProvider.credential(token)
f.auth().signInAndRetrieveDataWithCredential(credential).catch((error) => {
console.log(error)
})
var that = this;
const response = await fetch(`https://graph.facebook.com/me/?fields=id,name&access_token=${token}`);
const userInfo = await response.json();
this.setState({userInfo});
this.setState({
dataSource: userInfo.data,
isLoading: false,
});
} else {
// type === 'cancel'
}
} catch ({ message }) {
alert(`Facebook Login Error: ${message}`);
}
}
有人可以帮助我并给我一些提示,告诉我如何在我的应用程序中的任何地方使用访问 token 吗?提前谢谢你
最佳答案
token
并将其保存到AsyncStorage
那么你写的代码基本上是正确的。您已成功获得访问 token 。当您发出 Expo.Facebook.logInWithReadPermissionsAsync
请求时,它会返回给您。一旦你拥有它,你就可以将它存储在 Redux 或 AsyncStorage 中以备日后使用。
logIn = async () => {
let appID = '123456789012345' // <- you'll need to add your own appID here
try {
const {
type,
token, // <- this is your access token
expires,
permissions,
declinedPermissions,
} = await Expo.Facebook.logInWithReadPermissionsAsync(appID, { permissions: ['public_profile', 'email'], });
if (type === 'success') {
// Get the user's name using Facebook's Graph API
const response = await fetch(`https://graph.facebook.com/me/?fields=id,name&access_token=${token}`); //<- use the token you got in your request
const userInfo = await response.json();
alert(userInfo.name);
// you could now save the token in AsyncStorage, Redux or leave it in state
await AsyncStorage.setItem('token', token); // <- save the token in AsyncStorage for later use
} else {
// type === 'cancel'
}
} catch ({ message }) {
alert(`Facebook Login Error: ${message}`);
}
}
app.json
还记得将以下内容添加到您的 app.json
中,显然用您自己的值替换这些值。你可以通过在 Facebook 上注册你的应用程序来获得这些,你可以在这里看到更多相关信息 https://docs.expo.io/versions/latest/sdk/facebook/#registering-your-app-with-facebook
{
"expo": {
"facebookScheme": "fb123456789012345",
"facebookAppId": "123456789012345", // <- this is the same appID that you require above when making your initial request.
"facebookDisplayName": "you_re_facebook_app_name",
...
}
}
AsyncStorage
获取token
然后如果你想在以后发出另一个请求,你可以有一个类似于这个的函数,你从 AsyncStorage
中获取 token
然后用它来提出您的要求。
makeGraphRequest = async () => {
try {
let token = await AsyncStorage.getItem('token'); // <- get the token from AsyncStorage
const response = await fetch(`https://graph.facebook.com/me/?fields=id,name&access_token=${token}`); // <- use the token for making the graphQL request
const userInfo = await response.json();
alert(userInfo.name)
} catch (err) {
alert(err.message)
}
}
我会做点心来向您展示这个工作原理,但是,点心不允许编辑 app.json
文件(据我所知)。所以这里有一些你可以替换你的 App.js
的东西,然后如果你将你的 appIDs 等添加到 app.json
它应该可以工作。
import React from 'react';
import { AsyncStorage, Text, View, StyleSheet, SafeAreaView, Button } from 'react-native';
export default class App extends React.Component {
logIn = async () => {
let appID = '123456789012345' // <- you'll need to add your own appID here
try {
const {
type,
token, // <- this is your access token
expires,
permissions,
declinedPermissions,
} = await Expo.Facebook.logInWithReadPermissionsAsync(appID, {
permissions: ['public_profile', 'email'],
});
if (type === 'success') {
// Get the user's name using Facebook's Graph API
const response = await fetch(`https://graph.facebook.com/me/?fields=id,name&access_token=${token}`); //<- use the token you got in your request
const userInfo = await response.json();
console.log(userInfo);
alert(userInfo.name);
// you could now save the token in AsyncStorage, Redux or leave it in state
await AsyncStorage.setItem('token', token); // <- save the token in AsyncStorage for later use
} else {
// type === 'cancel'
}
} catch ({ message }) {
alert(`Facebook Login Error: ${message}`);
}
}
makeGraphRequest = async () => {
try {
let token = await AsyncStorage.getItem('token');
// Get the user's name using Facebook's Graph API
const response = await fetch(`https://graph.facebook.com/me/?fields=id,name&access_token=${token}`);
const userInfo = await response.json();
alert(userInfo.name)
} catch (err) {
alert(err.message)
}
}
render() {
return (
<View style={styles.container}>
<Button title={'Sign in to Facebook'} onPress={this.logIn} />
<Button title={'Make GraphQL Request'} onPress={this.makeGraphRequest} />
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'white'
}
});
关于react-native - 如何使用 Expo 获取 FB Access Token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54735944/
我有问题,我想在那个应用程序中使用 FB 好友列表,我可以通过 FB SDK 检索 FB 好友列表...... 最佳答案 FB SDK 支持通过以下方式调用 FB graph api: - (FBRe
我有一个简单的视频,最后包含一个共享按钮,当用户单击共享按钮时,会打开一个带有社交媒体图标的模态框,例如用户可以将视频共享到 Facebook,现在我想要来自 Facebook 的回调响应视频分享后。
我收到常见的 JS 错误“ Uncaught ReferenceError :未定义 FB” 关于同一个问题有很多问题,我已经尝试了每个可用的解决方案 stackoverflow 但无济于事。显然,人
你好,我想知道如果我创建了一个 Facebook 页面(没有普通用户)是否可以获得 FB APP_ID,或者这个 ID 仅用于应用程序?谢谢 最佳答案 如果您是 Facebook 用户,请登录您的 I
我有一个客户已经有一个与他们自己的网站相关联的 Facebook 页面。我最终说服他们将开放图标签集成到他们的页面中,因为当任何人点击他们网站上的 Facebook Like 按钮时,它在 Faceb
我的问题很简单,FB Messenger如何知道谁与其他FB应用程序连接,我的意思是在iOS中,应用程序使用sandBox,但是当您在“正常”应用程序中作为“xy”连接并且打开Messenger时它会
这段最近工作完美的代码在与 FB JS API 一起使用时不再正常工作: placeOrder : function(order_info) { var onOrderPlaced = funct
我正在尝试从我使用以下代码的 facebook 广告 api 获取广告系列列表 $account = new AdAccount('act_' . $account_id);
我的控制台中有这条消息 FB.getLoginStatus() called before calling FB.init(). 我的代码 // Load the SDK Asyn
我知道有很多关于 Facebook 访问 token 的问题及其造成的痛苦,但尽管进行了大量实验并阅读了许多令人沮丧的模糊博客文章(FB 和其他),我仍在努力获得明确的答案根据我的需要。让我简要地分解
我查看了有关 stackoverflow 的各种文档和问题,但仍然无法弄清楚为什么以下代码不会触发任何 FB.Event.subscribe 事件 getLoginStatus。 (fun
我想要实现的是: 我有一个简单的 Facebook 分享按钮,取自 Facebook 开发人员。站点: 现在我在开始正文标记之前添加了同样取自 facebook 的 facebook 应用程序代码:
我想检查用户是否使用 Facebook JavaScript JDK 和 jQuery 登录 facebook。但即使超时 10 秒,我也会收到此错误消息: 在调用 FB.init() 之前调用 FB
我有一个正在编写的网站。 javascript SDK 有效 php SDK 有效 HTML 如下所示: 带有 fb-registration div 的注册页面 带有 fb-login-button
我正在尝试检查用户是否已使用我的应用登录,但我收到了 FB.getLoginStatus() called before calling FB.init(). 控制台出错。 setSize似乎有效(虽
FB.api 在 FB.init 之后立即调用时不起作用。这是我使用的代码片段: window.fbAsyncInit = function() { FB.init({ appId : w
我希望用户在单击我网页上的几个按钮之一时能够将一些图像发布到他们的 Facebook 页面。 FB.getLoginStatus(function(response) { if ( respon
我正在使用 PFFacebookUtils.logInWithPermissions 登录我的应用程序,但是,第一次登录实际登录工作正常,但出现 FB 错误 code = 2500 An active
我全新的工作充满了美妙和可怕的惊喜。这项工作最有趣的部分之一是增强、加速、使一切规模化的意愿。今天,第一个真正的问题。事情是这样的:我们得到最多 20 个列表元素,每个元素都显示自己的 Faceboo
我在这里使用 asp.net web 应用程序,我在我的网页中使用 2 个 facebook 插件。 Facebook 赞 Facebook 分享 对于 facebook 分享: Faceboo
我是一名优秀的程序员,十分优秀!