更新:即使这样的请求也会得到错误的凭据==>
curl -H "Authorization: token [token]" https://api.github.com
Run Code Online (Sandbox Code Playgroud)
===============
我在我的 iOS 应用程序中向 GitHub OAuth 发出了这样的请求:
URL: https://github.com/login/oauth/authorize,
params: client_id: ****, redirect_uri: app_url, scope: "repo", state: "0"
Run Code Online (Sandbox Code Playgroud)
从 Safari 重定向后,我得到一个代码。我需要访问两件事:
我提出以下要求:
URL: https://api.github.com/user,获取并修补 Header: Authorization: token [code]
URL: https://api.github.com/repos/:username/:repoName
Header: Authorization: token [code]
Run Code Online (Sandbox Code Playgroud)
但不幸的是,我收到以下错误:
401, Unauthorized
{
"message": "Bad credentials",
"documentation_url": "https://developer.github.com/v3"
}
Run Code Online (Sandbox Code Playgroud)
问题是什么?我该如何解决?
我有一个现有的注册/登录系统:用户输入电子邮件和密码。密码经过哈希处理。我将其存储在数据库中。
当用户登录时,他们会填写电子邮件和密码。密码经过哈希处理,我在数据库中查找电子邮件并检查电子邮件是否匹配。如果是,则他们已登录。
我想添加一个系统,让用户使用第三方 OAuth 登录,例如 GitHub。我有这样的设置,但我不确定要在数据库中存储哪些数据。
我想我将他们的 GitHub 电子邮件作为电子邮件,然后使用他们的 GitHub 的访问令牌作为密码(所以我会对它进行哈希处理并存储它。)
我认为这会起作用,但我担心访问令牌可能会发生变化,这意味着他们将被锁定在帐户之外。
如果我不应该使用访问令牌作为密码,我应该使用什么?我需要将用户的电子邮件存储在我的数据库中,但目前需要密码,如果他们使用 GitHub 登录,我将无法获得密码。
(请注意,当用户登录时,我调用我的后端来生成一个访问令牌(JWT),我可以使用它来获取他们的用户详细信息,然后将其存储在本地存储中。我希望能够执行相同的操作与 GitHub 或其他什么有关。)
我正在尝试构建一个简单的应用程序,目前仅使用 GitHub 登录进行身份验证。我使用 Rails v5.2.3 作为后端,使用 React 作为前端。目前,我的根组件中有一个按钮,用于向后端发送 ajax 请求。该请求将触发 my 中的一个操作,SessionsController该操作将重定向到/auth/github路由并开始 GitHub 身份验证周期。
我相信这一步是我遇到错误的地方。
我的浏览器控制台向我显示以下错误消息:
Access to XMLHttpRequest at 'https://github.com/login/oauth/authorize?
client_id=db494fb7eadbc0c6129d&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fauth%2Fgithub%2Fcallback&resp
onse_type=code&state=79557eda02a2340f9c02b5254f053528314ea750704690ae' (redirected from
'http://localhost:3000/authenticate/github') from origin 'http://localhost:3000' has been blocked by CORS
policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Run Code Online (Sandbox Code Playgroud)
以下是我认为相关的一些文件:
Gemfile
Access to XMLHttpRequest at 'https://github.com/login/oauth/authorize?
client_id=db494fb7eadbc0c6129d&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fauth%2Fgithub%2Fcallback&resp
onse_type=code&state=79557eda02a2340f9c02b5254f053528314ea750704690ae' (redirected from
'http://localhost:3000/authenticate/github') from origin 'http://localhost:3000' has been blocked by CORS
policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Run Code Online (Sandbox Code Playgroud)
routes.rb
source …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个网站,允许用户授权其 GitHub 帐户,然后允许用户单击按钮create。它会自动创建一个包含一些文件的新存储库。现在我很困惑我应该为此制作一个 GitHub 应用程序还是 OAuth 应用程序。我已经查看了文档,但仍然很困惑。我将托管该网站,这将允许任何拥有 GitHub 帐户的随机用户。最好的选择是什么?
github-oauth ×4
github ×2
oauth ×2
axios ×1
cors ×1
database ×1
github-api ×1
github-app ×1
oauth-2.0 ×1
reactjs ×1