简体   繁体   中英

GitHub request get a repository

How can I make a get request to a repository if my user hasn't generated a personal token and I have just the access token from authentication with oauth2? I have tried some options with postman but I can't fix it. I want to access a private repository

What I'm trying to do:

require "uri"
require "net/http"

url = URI("https://api.github.com/repos/username/repository_name")

https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true

request = Net::HTTP::Get.new(url)
request["Authorization"] = "Bearer ACCESS_TOKEN"
request["Cookie"] = "_octo=GH1.1.1832634711.1663350372; logged_in=no"

response = https.request(request)
puts response.read_body

One of my collaborators resolve in this way, I hope to be helpful to someone else

  def get_github_link
    @group=Group.find(params[:id])
    if current_user.gh_access_token.nil?
      flash[:notice] = "You havn't your GithHub account linked, this is a private repository!"
      redirect_to @group and return
    end
    if @group.status=="private"
      url = URI.parse("https://api.github.com/repos/#{current_user.gh_username}/#{@group.git_repository}")
      https = Net::HTTP.new(url.host, url.port)
      https.use_ssl = true
      request = Net::HTTP::Post.new(url)
      request["Authorization"] = "token #{current_user.gh_access_token}"
      request["Content-Type"] = 'application/json'
      request["Accept"] = 'application/vnd.github+json'
      request["Coockies"] = 'login-yes'
      request.body = {owner: @group.user.gh_username}.to_json
      response = https.request(request)
    else
      url = URI.parse("https://api.github.com/repos/#{@group.user.gh_username}/#{@group.git_repository}")
          https = Net::HTTP.new(url.host, url.port)
        https.use_ssl = true
        request = Net::HTTP::Get.new(url)
        request["Authorization"] = "token #{current_user.gh_access_token}"
        request["Accept"] = 'application/vnd.github+json'
        request.body = {owner: @group.user.gh_username}.to_json
        response = https.request(request)
    end
    #url = URI.parse("https://api.github.com/repos/"+@group.git_url.remove("https://github.com/"))
    
    
    json = JSON.parse(response.body, symbolize_names: true)   
    if eval(response.code.to_s) === 200
      redirect_to @group.git_url, allow_other_host: true and return
    else
      flash[:notice] = "You have not the access to this repository, please conctact the admin"
      redirect_to @group and return
    end
    
  end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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