简体   繁体   English

POST请求到GitHub API

[英]POST request to GitHub API

I'm having trouble making a POST request to the GitHub API using the JavaScript fetch method: 我在使用JavaScript提取方法向GitHub API发出POST请求时遇到了麻烦:

fetch('https://api.github.com/repos/organization/repo/issues?client_id=CLIENT_ID&client_secret=CLIENT_SECRET', {
      method: 'post',
      body: {
        title: 'Title',
        body: {body: "body", title: "title"}
      }
    })

I am using a client ID and a client secret that I got from registering the application with the GitHub API: 我正在使用从GitHub API注册应用程序时获得的客户端ID和客户端密码:

在此处输入图片说明

Any help would be greatly appreciated! 任何帮助将不胜感激! Thank you! 谢谢!

I guess you need access token to access Github API. 我想您需要访问令牌才能访问Github API。 If you want to try manually here's my suggestion steps. 如果您想手动尝试,这是我的建议步骤。 I will try to explain from the first step. 我将尝试从第一步开始进行解释。


  1. Register your app. 注册您的应用。

    On your github account, go to settings -> OAuth Applications 在您的github帐户上,转到settings -> OAuth Applications

    This is the image when you register your application 这是您注册应用程序时的图像


  1. Get the Client ID and Client Secret. 获取客户端ID和客户端密钥。

    This is the image after you receive Client ID and Client Secret 这是收到客户端ID和客户端密钥后的图像


  1. Ask for Github Code 要求Github代码

    Now you have Client ID . 现在您有了Client ID Go to this url. 转到此URL。

    https://github.com/login/oauth/authorize?client_id=b420627027b59e773f4f&scope=user:email,repo

    Please define your own client_id and scope . 请定义您自己的client_idscope


  1. Get the Github Code 获取Github代码

    Remember the Authorization callback URL you input when register? 还记得注册时输入的授权回调URL吗? After you go to the link above, you should have redirected to your callback URL with the code as the parameter. 转到上面的链接后,您应该已使用代码作为参数重定向到回调URL。

    For example http://localhost:8080/github/callback?code=ada5003057740988d8b1 例如http://localhost:8080/github/callback?code=ada5003057740988d8b1


  1. Ask and Get the Access Token 询问并获取访问令牌

    Now you need to do http request post with Client ID , Client Secret , and Code you have got as the parameter. 现在,您需要使用已获取的Client IDClient SecretCode作为参数进行http请求发布。

    Request 请求

    POST https://github.com/login/oauth/access_token?client_id=a989cd9e8f0137ca6c29&client_secret=307d18600457b8d9eec1efeccee79e34c603c54b&code=ada5003057740988d8b1

    Response 响应

    access_token=e72e16c7e42f292c6912e7710c838347ae178b4a&token_type=bearer


  1. Post Issue to Github 发布到Github

    Now you have access token you can use it to access Github API. 现在,您具有access token ,可以使用它访问Github API。

 fetch('https://api.github.com/repos/organization/repo/issues?access_token=e72e16c7e42f292c6912e7710c838347ae178b4a', { method: 'post', body: { title: 'Title', body: {body: "body", title: "title"} } }) 

To achieve what you want you have to implement the web application flow described here . 要实现您想要的目标,您必须实现此处描述的Web应用程序流程。

This means you have to redirect the user to https://github.com/login/oauth/authorize?client_id=CLIENT_ID&redirect_uri=REDIRECT_URI so that he can login to GitHub and authorize your application. 这意味着您必须将用户重定向到https://github.com/login/oauth/authorize?client_id=CLIENT_ID&redirect_uri=REDIRECT_URI以便他可以登录GitHub并授权您的应用程序。 After successful login GitHub redirects you to the redirect_uri , which usually points to an endpoint of your application. 成功登录后,GitHub会将您重定向到redirect_uri ,该地址通常指向应用程序的端点。 This endpoint extracts the authorization code from the URI to request an access token from GitHub with it (See here ). 该端点从URI中提取授权代码,以使用GitHub向GitHub请求访问令牌(请参阅此处 )。 As soon as you have the access token you can consume the GitHub API by sending the OAuth token in the Authorization header as follows. 拥有访问令牌后,您可以通过在Authorization标头中发送OAuth令牌来使用GitHub API,如下所示。

Authorization: token OAUTH-TOKEN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM