我正在使用Expo来通过Facebook登录用户,我正在使用Graph Api接收令牌,但是当我尝试在Async Storage中添加令牌时,它无法正常工作。
请参见下面的代码:
async logIn() {
try {
const {
type,
token,
} = await Facebook.logInWithReadPermissionsAsync('<APP_ID>', {
permissions: ['public_profile'],
});
if (type === 'success') {
// Get the user's name using Facebook's Graph API
fetch(`https://graph.facebook.com/me?access_token=${token}`)
.then((res) => res.json())
.then((tokenKey) => AsyncStorage.setItem('userToken',tokenKey))
.then(() => this.props.navigation.navigate('App'))
} else {
// type === 'cancel'
}
} catch ({ message }) {
alert(`Facebook Login Error: ${message}`);
}
}
我控制台时收到令牌
fetch(`https://graph.facebook.com/me?access_token=${token}`)
.then((res) => res.json())
.then((tokenKey) => console.log('userToken',tokenKey))
.then(() => this.props.navigation.navigate('App'))
请帮助,我是新来响应JavaScript中的本机和异步编程的人。 TIA :)
答案 0 :(得分:2)
如果要从AsyncStorage获取项目,请尝试此操作
AsyncStorage.getItem('userToken', (error, result) => {
if (result) {
//result
}
else {
//error
}
});
答案 1 :(得分:1)
您是通过getItem从AsyncStorage获取令牌吗?
# ---------------------------------- USER CART API -----------------------------
@app.route('/pricing/orders/<user_name>/api', methods=['POST', 'GET'])
@login_required
def api(user_name):
user_name = current_user.username
if request.method == 'POST':
cart.append(json.loads(request.form["javascript_data"]))
return jsonify(cart)
# ---------------------------- DELETE ITEM IN CART ROUTE ----------------------------------
@app.route('/pricing/orders/delete', methods=['POST', 'GET'])
@login_required
def delete_item():
if request.method == 'POST':
print(cart[json.loads(request.form["delete_item"])])
cart.pop(json.loads(request.form["delete_item"]))
print(cart)
return jsonify({"whoa": "there"})
# ----------------------------- DISPLAY CART BADGE LENGTH---------------------------------
@app.context_processor
def inject_badge_length():
badge_length = len(cart)
return {'BADGE_LENGTH' : badge_length}
答案 2 :(得分:0)
对不起,问题出在我这边,我试图将对象直接存储到 Async Storage 中,而Async Storage仅接受 String 格式的值。我用过
.then((tokenKey) => AsyncStorage.setItem('userToken',JSON.stringify(tokenKey)))
它解决了这个问题,谢谢大家的帮助