我尝试使用react-native和redux实现facebook登录,但我遇到了问题:
在我的控制台中,我拥有用户的所有信息,但在redux的对象中,authToken未定义,我不明白为什么.. 这是我的代码
应用/ SRC / facebook.js
import requests
from bs4 import BeautifulSoup as soup
import csv
url = 'https://www.saa.gov.uk/search.php?SEARCHED=1&SEARCH_TABLE=valuation_roll_cpsplit&SEARCH_TERM=edinburgh%2C+GOGARBANK%2C+EDINBURGH%2C+Edinburgh%2C+City+Of&x=16&y=8&DISPLAY_COUNT=10&ASSESSOR_ID=&TYPE_FLAG=CP&ORDER_BY=PROPERTY_ADDRESS&H_ORDER_BY=SET+DESC&DRILL_SEARCH_TERM=GOGARBANK%2C+EDINBURGH%2C+Edinburgh%2C+City+Of&DD_UNITARY_AUTHORITY=Edinburgh%2C+City+Of&DD_TOWN=EDINBURGH&DD_STREET=GOGARBANK&DISPLAY_MODE=FULL&UARN=103G494E2%28B%29&PPRN=000000000000532&ASSESSOR_IDX=10&#results'
baseurl = 'https://www.saa.gov.uk'
session = requests.session()
response = session.get(url)
# content of search page in soup
html = soup(response.content,"lxml")
# list of result entries
rslt_table = html.find("table", {"summary":"Property details"})
ref = 'n/a'
vsr = 'n/a'
for col in rslt_table:
elems = col.find("th")
data = col.find("td")
#for i in range(len(elems)):
for i in range(elems):
if elems [i].text == "Ref No. / Office":
ref = data[i].text
print ref
if elems [i].text == 'Description':
vsr = data[i].text
print vsr
操作(所有操作类型都在另一个文件中)
import {
LoginManager,
AccessToken,
GraphRequest,
GraphRequestManager,
} from 'react-native-fbsdk';
const facebookParams = 'id,name,email,picture.width(100).height(100)';
export function facebookLoginAPI() {
return new Promise((resolve, reject) => {
LoginManager.logInWithReadPermissions(['public_profile', 'user_friends', 'email'])
.then((FBloginResult) => {
if (FBloginResult.isCancelled) {
throw new Error('Login cancelled');
}
if (FBloginResult.deniedPermissions) {
throw new Error('We need the requested permissions');
}
return AccessToken.getCurrentAccessToken();
console.log(FBloginResult);
})
.then((result) => {
resolve(result);
console.log(result);
})
.catch((error) => {
reject(error);
console.log(error);
});
});
}
export function getFacebookInfoAPI() {
return new Promise((resolve, reject) => {
const profileInfoCallback = (error, profileInfo) => {
if (error) reject(error);
resolve(profileInfo);
};
const profileInfoRequest =
new GraphRequest(
'/me',
{
parameters: {
fields: {
string: facebookParams,
},
},
},
profileInfoCallback
);
new GraphRequestManager().addRequest(profileInfoRequest).start();
});
}
export function getFacebookFriends() {
return new Promise((resolve, reject) => {
const profileInfoCallback = (error, profileInfo) => {
if (error) reject(error);
console.log(profileInfo);
resolve(profileInfo);
};
const profileFriendsRequest =
new GraphRequest(
'/me/friends',
{
parameters: {
fields: {
string: facebookParams,
},
},
},
profileInfoCallback
);
new GraphRequestManager().addRequest(profileFriendsRequest).start();
});
}
和减速机:
import { facebookLoginAPI, getFacebookInfoAPI } from '../src/facebook';
import { getServerAuthToken } from '../src/auth';
import {
AUTH_STARTED,
AUTH_SUCCESS,
AUTH_FAILURE,
AUTH_ERROR,
AUTH_FAILURE_REMOVE,
LOGOUT
} from './types';
export function authStarted() {
return {
type: AUTH_STARTED,
};
}
export function authSuccess(facebookToken, facebookProfile, serverAuthToken){
return {
type: AUTH_SUCCESS,
facebookToken,
facebookProfile,
authToken: serverAuthToken,
};
}
export function authFailure(authError){
return {
type: AUTH_FAILURE,
authError,
};
}
export function authFailureRemove() {
return {
type: AUTH_FAILURE_REMOVE,
};
}
export function logout() {
return {
type: LOGOUT,
};
}
export function facebookLogin() {
return (dispatch) => {
dispatch(authStarted());
const successValues = [];
facebookLoginAPI()
.then((facebookAuthResult) => {
[...successValues, ...facebookAuthResult.accessToken];
return getFacebookInfoAPI(facebookAuthResult.accessToken);
}).then((facebookProfile) => {
[...successValues, ...facebookProfile];
return getServerAuthToken();
}).then((serverAuthToken) => {
[...successValues, ...serverAuthToken];
dispatch(authSuccess(...successValues));
}).catch((error) => {
dispatch(authFailure(error));
setTimeout(() => {
dispatch(authFailureRemove());
}, 4000);
});
};
}
我需要了解什么是authToken,为什么我的情况不明确?认证是否成功..我不知道!
谢谢!
答案 0 :(得分:1)
下面的代码看起来对我来说很可疑
export function facebookLogin() {
return (dispatch) => {
dispatch(authStarted());
const successValues = [];
facebookLoginAPI()
.then((facebookAuthResult) => {
[...successValues, ...facebookAuthResult.accessToken]; //remove this line
return getFacebookInfoAPI(facebookAuthResult.accessToken);
}).then((facebookProfile) => {
[...successValues, ...facebookProfile]; //remove this seems of no use
return getServerAuthToken(); //I think you may need to pass something here
}).then((serverAuthToken) => {
[...successValues, ...serverAuthToken]; //pass this value in authSuccess below instead of ...successValues (it may still be [])
dispatch(authSuccess(...successValues));
}).catch((error) => {
dispatch(authFailure(error));
setTimeout(() => {
dispatch(authFailureRemove());
}, 4000);
});
};
}