Reactjs 如何从react中的gapi获取配置文件数据
我正在尝试从谷歌api获取数据,我可以让登录工作,我可以获得id,但我无法获得电子邮件或任何其他用户信息 这是我的GoogleAuth文件Reactjs 如何从react中的gapi获取配置文件数据,reactjs,redux,google-api-js-client,Reactjs,Redux,Google Api Js Client,我正在尝试从谷歌api获取数据,我可以让登录工作,我可以获得id,但我无法获得电子邮件或任何其他用户信息 这是我的GoogleAuth文件 class GoogleAuth extends React.Component{ componentDidMount() { window.gapi.load('client:auth2', () => { window.gapi.client.init({ clientId: 'my-client.
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.sign(this.auth.currentUser.get().getName())
可能没有正确放置,因为我得到一个错误
TypeError:this.auth.currentUser.get(…).getName不是函数
我从谷歌开发者网站上找到了我,但我找不到任何关于react的信息,只有如何登录,但我还需要使用这些信息
更新
这几乎奏效了:
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"
虽然我不确定这是否是您问题的直接解决方案,但是将
this.auth.currentUser.get().getName()
修改为this.auth.currentUser.get().getBasicProfile().getName()
?如果这仍然不起作用,我道歉。感谢它几乎起作用。更多信息请参阅更新@TanaikeThank谢谢你的回复。我很高兴你的问题得到了解决。它还没有完全解决,现在我只需要将用户ID更改为先是用户ID,然后是姓名,然后是电子邮件,我无法访问姓名谢谢你的回复。我为我糟糕的英语水平道歉。不幸的是,从您对的回复中,我无法理解您当前的问题,现在我只需将用户ID更改为先是用户ID,然后是名称,然后是电子邮件,我无法访问名称
。我能问一下细节吗?