我正在尝试从Google api获取数据,我可以使登录名正常工作并且可以获取ID,但无法获取电子邮件或任何其他用户信息
这是我的GoogleAuth文件
class GoogleAuth extends React.Component{
componentDidMount() {
window.gapi.load('client:auth2', () => {
window.gapi.client.init({
clientId: 'my-client.id.apps.googleusercontent.com',
scope: 'profile email',
'onsuccess': this.onSignIn
}).then(() => {
this.auth = window.gapi.auth2.getAuthInstance();
this.onAuthChange(this.auth.isSignedIn.get());
this.auth.isSignedIn.listen(this.onAuthChange);
this.auth2.currentUser.get().getBasicProfile();
});
});
}
onAuthChange = (isSignedIn) => {
if (isSignedIn) {
this.props.signIn(this.auth.currentUser.get().getId());
console.log(this.props.signIn(this.auth.currentUser.get().getName()));
} else{
this.props.signOut();
}
};
我知道this.auth2.currentUser.get().getBasicProfile();
和console.log(this.props.signIn(this.auth.currentUser.get().getName()));
的位置可能不正确,因为我在输入
TypeError:this.auth.currentUser.get(...)。getName不是函数
我从google开发人员网站上获得了我,但是我只能在如何登录上找不到任何反应,但是我还需要使用信息
更新 这几乎奏效了:
class GoogleAuth extends React.Component{
componentDidMount() {
window.gapi.load('client:auth2', () => {
window.gapi.client.init({
clientId: 'my-client.id.apps.googleusercontent.com',
scope: 'profile email',
'onsuccess': this.onSignIn
}).then(() => {
this.auth = window.gapi.auth2.getAuthInstance();
this.onAuthChange(this.auth.isSignedIn.get());
this.auth.isSignedIn.listen(this.onAuthChange);
});
});
}
onAuthChange = (isSignedIn) => {
if (isSignedIn) {
this.props.signIn(this.auth.currentUser.get().getId());
console.log(this.props.signIn(this.auth.currentUser.get().getBasicProfile().getName()));
console.log(this.props.signIn(this.auth.currentUser.get().getBasicProfile().getEmail()));
} else{
this.props.signOut();
}
};
但是现在每个日志都覆盖另一个日志,而userId是唯一被更改的日志
{type: "SIGN_IN", payload: {…}}
payload: {userId: "Thor Hammervig", email: undefined}
type: "SIGN_IN"
{type: "SIGN_IN", payload: {…}}
payload: {userId: "thor.tordenmis@gmail.com", email: undefined}
type: "SIGN_IN"