gpt4 book ai didi

javascript - 使用 Meteor 的自定义 Github 登录不起作用

转载 作者:行者123 更新时间:2023-11-29 14:55:16 24 4
gpt4 key购买 nike

我一直在逐步遵循此链接中显示的 Eventedmind 的自定义登录教程:

https://www.eventedmind.com/posts/meteor-customizing-login

所以我在 Github 上创建了一个新的应用程序并对其进行了编码,但是在运行 Meteor 时我仍然有错误并且没有任何显示。我知道服务器端有错误,但我不知道是什么,也许代码写得不好,或者我调用登录的方式不再正确。这是我所做的(我猜与教程中的相同)

客户端/index.html

<head>
<title>App</title>
</head>

<body>
{{> header}}
</body>

<template name="header">
<div class="navbar navbar-inverse">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="#">NewApp</a>
<form class="navbar-search pull-left">
<input type="text" class="search-query" placeholder="Search">
</form>
<div class="nav pull_right">
{{> user_info}}
</div>
</div>
</div>
</div>
</template>

<template name="user_info">
<ul class="nav pull-right">
{{#if currentUser}}
{{> user_loggedin}}
{{else}}
{{> user_loggedout}}
{{/if}}
</ul>
</template>


<template name="user_loggedin">
{{#if loggingIn}}
<li><a href="">Loggin in...</a></li>
{{else}}
<li>
<img src="{{currentUser.profile.avatar_url}}" class="img-rounded" style="height: 32px; margin-top: 4px;">
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
{{currentUser.profile.login}}
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="">Accounts Settings</a></li>
<li class="divider"></li>
<li><a id="logout">logout</a></li>
</ul>
</li>
{{/if}}
</template>


<template name="user_loggedout">
<li><a id="login">Login with Github</a></li>
</template>

客户端/index.js

Template.user_loggedout.events({
'click #login': function (e, tmpl) {
Meteor.loginWithGithub({
requestPermissions: ['user', 'public_repo']
}, function (err) {
if (err) {
// error handling
} else {
// show alert
}
});
}
});

Template.user_loggedin.events({
'click #logout': function (e, tmpl) {
Meteor.logout(function (e, tmpl) {
if (err) {
// show err message
} else{
// show alert that says logged out
}
});
}
});

服务器/config.js

Accounts.loginServiceConfiguration.remove({
service: "github"
});

Accounts.loginServiceConfiguration.insert({
service: "github",
clientId: "NUMBER",
secret: "SECRET_NUMBER"
});

服务器/accounts.js

Accounts.onCreateUser(function (options, user) {
var accessToken = user.services.github.accessToken,
result,
profile;
result = Meteor.http.get("https://api.github.com/user", {
params: {
access_token: accessToken
}
});
if (result.error)
throw result.error;

profile = _.pick(result.data,
"login",
"name",
"avatar_url",
"url",
"company",
"blog",
"location",
"email",
"bio",
"html_url");

user.profile = profile;

return user;
});

最佳答案

最新的 github API 要求在每个请求中都有一个 HTTP User-Agent header 。只需将其包含在下面即可。

result = Meteor.http.get("https://api.github.com/user", {
headers: {"User-Agent": "Meteor/1.0"},
params: {
access_token: accessToken
}
});

关于javascript - 使用 Meteor 的自定义 Github 登录不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18805740/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com