GitHub commits API(nodejs)not found when repository is private如何传递凭据

wrrgggsh  于 12个月前  发布在  Node.js
关注(0)|答案(1)|浏览(122)

我正在使用API创建一个API到GitHub提交,使用API列出提交。我怀疑我有一个存储库,如果存储库是公共的,API可以完美工作。如果我将其更改为私有的,它会给出“notfound”消息。访问的电子邮件不会在Google中获得“notfound”,所以我怎么能克服这一点,而在私人的仓库的路线是找不到的。我有电子邮件ID的访问,我怎么能通过它使用API?

const express = require('express');
const { Octokit } = require('@octokit/core');

const app = express();
const port = 3000;

app.get('/api/commits', async (req, res) => {
  try {
    const octokit = new Octokit({
      auth: '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',
    });

    const owner = 'Praveenkumarsaravana';
    const repo = 'django';

    const response = await octokit.request('GET /repos/{owner}/{repo}/commits', {
      owner: owner,
      repo: repo,
      headers: {
        'X-GitHub-Api-Version': '2022-11-28',
      },
    });

    res.json(response.data);
} catch (error) {
    console.error('Error:', error.message);
    console.error('Error details:', error.response?.data);
    res.status(500).json({ error: 'Internal Server Error' });
  }
});

app.get('/api/commit/:sha/branches', async (req, res) => {
  try {
    const octokit = new Octokit({
      auth: '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',
    });

    const owner = 'Praveenkumarsaravana';
    const repo = 'django';
    const commit_sha = req.params.sha;

    const response = await octokit.request('GET /repos/{owner}/{repo}/commits/{commit_sha}/branches-where-head', {
      owner: owner,
      repo: repo,
      commit_sha: commit_sha,
      headers: {
        'X-GitHub-Api-Version': '2022-11-28',
      },
    });

    res.json(response.data);
  } catch (error) {
    console.error('Error:', error.message);
    res.status(500).json({ error: 'Internal Server Error' });
  }
});
// Get pull requests associated with a specific commit
app.get('/api/commit/:sha/pulls', async (req, res) => {
    try {
        console.log('Received request for /api/commit/:sha/pulls');
      const octokit = new Octokit({
        auth: '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',
      });
  
      const owner = 'Praveenkumarsaravana';
      const repo = 'django';
      const commit_sha = req.params.sha;
  
      const response = await octokit.request('GET /repos/{owner}/{repo}/commits/{commit_sha}/pulls', {
        owner: owner,
        repo: repo,
        commit_sha: commit_sha,
        headers: {
          'X-GitHub-Api-Version': '2022-11-28',
        },
      });
  
      res.json(response.data);
    } catch (error) {
      console.error('Error:', error.message);
      res.status(500).json({ error: 'Internal Server Error' });
    }
  });
  
  app.post('/api/commit/details/:sha', async (req, res) => {
    try {
      const octokit = new Octokit({
        auth: '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',
      });
  
      const owner = 'Praveenkumarsaravana';
      const repo = 'django';
  
      // Check if req.body and req.body.ref are defined
    //   if (!req.body || !req.body.ref) {
    //     return res.status(400).json({ error: 'Missing or undefined "ref" in the request body' });
    //   }
  
      const ref = req.params.ref;
  
      const response = await octokit.request('GET /repos/{owner}/{repo}/commits/{ref}', {
        owner: owner,
        repo: repo,
        ref: ref,
        headers: {
          'X-GitHub-Api-Version': '2022-11-28',
        },
      });
  
      res.json(response.data);
    } catch (error) {
      console.error('Error:', error.message);
      res.status(500).json({ error: 'Internal Server Error' });
    }
  });
  app.post('/api/compare/:basehead', async (req, res) => {
    try {
      const octokit = new Octokit({
        auth: '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',
      });

      const owner = 'Praveenkumarsaravana';
      const repo = 'django';
      const basehead = req.params.basehead;
  
    //   if (!req.body || !req.body.basehead) {
    //     return res.status(400).json({ error: 'Missing or undefined "basehead" in the request body' });
    //   }
  
      const response = await octokit.request('GET /repos/{owner}/{repo}/compare/{basehead}', {
        owner: owner,
        repo: repo,
        basehead: basehead,
        headers: {
          'X-GitHub-Api-Version': '2022-11-28',
        },
      });
  
      res.json(response.data);
    } catch (error) {
      console.error('Error:', error.message);
      res.status(500).json({ error: 'Internal Server Error' });
    }
  });
  
  

app.listen(port, () => {
  console.log(`Server is running on http://localhost:${port}`);
});

字符串
这是完整的index.js页面
http://localhost:3000/api/commits上面是一个函数的路由,如果仓库是私有的,它的notfound我怎么能传递凭证?

nwwlzxa7

nwwlzxa71#

首先,从代码中删除类似于以下位置的令牌:

const octokit = new Octokit({
  auth: 'ghp_####################################',
});

字符串

现在

第二,看起来你在代码中没有错误,它是相当工作,试试这个:
代币编号:
确保您使用的个人访问令牌具有必要的权限。在您的GitHub帐户中转到Settings > Developer Settings > Personal access tokens,并确保令牌具有必要的权限。
代币范围:
如果修改令牌的权限,则可能需要重新生成并使用新令牌。
存储库访问:
确保与个人访问令牌关联的GitHub帐户可以访问私有存储库。如果您用于身份验证的电子邮件地址无法访问私有存储库,则无法访问它。
检查Typos:
仔细检查代码中的存储库nameowner和其他详细信息是否正确,并与您试图访问的私有存储库相匹配。

相关问题