Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用OAuth和Auth0进行身份验证后获取Github repo数据_Javascript_Github_Oauth_Github Api_Auth0 - Fatal编程技术网

Javascript 使用OAuth和Auth0进行身份验证后获取Github repo数据

Javascript 使用OAuth和Auth0进行身份验证后获取Github repo数据,javascript,github,oauth,github-api,auth0,Javascript,Github,Oauth,Github Api,Auth0,我想做的是从一个小应用程序中获取私有回购数据(发行量) 因为我不想在请求中暴露我的身份,所以我搜索了另一个解决方案,我认为它是OAuth 因此,我建立了一个OAuth跟踪和步骤 并将社交github设置中的权限设置为repo(授予repo的读/写访问权限) 到目前为止,一切正常,我能够通过我的github帐户进行身份验证 但是现在我不知道我必须使用哪个token来请求githubAPI 以下是迄今为止我掌握的相关代码: const handleAuthentication = () =>

我想做的是从一个小应用程序中获取私有回购数据(发行量)

因为我不想在请求中暴露我的身份,所以我搜索了另一个解决方案,我认为它是OAuth

因此,我建立了一个OAuth跟踪和步骤

并将社交github设置中的权限设置为
repo
(授予repo的读/写访问权限)

到目前为止,一切正常,我能够通过我的github帐户进行身份验证

但是现在我不知道我必须使用哪个
token
来请求github
API

以下是迄今为止我掌握的相关代码:

const  handleAuthentication = () => {
  webAuth.parseHash((err, authResult) => {
    if (authResult && authResult.accessToken && authResult.idToken) {
      window.location.hash = '';
      setSession(authResult);

      //here the call to the relevant function
      getIssues('my-element', /*here i should pass the correct token*/); //tryed all tokens i found in authResult

    } else if (err) {
      alert(
        `Error: ${err.error} . Check the console for further details.`
      );
    }
  });
}

const getIssues = (element, token) => {
  const request = new XMLHttpRequest();
  request.open('GET', `https://api.github.com/repos/my_organization/element-${element}/issues?access_token=${token}`, true);

  request.onload = () => {
    if (request.status >= 200 && request.status < 400) {
      const data = JSON.parse(request.responseText);
      //use the data
    } else {
      console.log('error');
    }
  };

  request.onerror = function(e) {
    console.log('connection error: ', e)
  };

  request.send();
}
const handleAuthentication=()=>{
webAuth.parseHash((错误,authResult)=>{
if(authResult&&authResult.accessToken&&authResult.idToken){
window.location.hash='';
设置会话(authResult);
//这里是对相关函数的调用
getIssues('my-element',/*这里我应该传递正确的令牌*/);//尝试在authResult中找到的所有令牌
}否则如果(错误){
警觉的(
`错误:${err.Error}。有关详细信息,请查看控制台`
);
}
});
}
const getIssues=(元素、令牌)=>{
const request=new XMLHttpRequest();
request.open('GET'`https://api.github.com/repos/my_organization/element-${element}/issues?access_token=${token}`,true);
request.onload=()=>{
如果(request.status>=200&&request.status<400){
const data=JSON.parse(request.responseText);
//使用数据
}否则{
console.log('error');
}
};
request.onerror=函数(e){
console.log('连接错误:',e)
};
request.send();
}
但这会导致401(未经授权)响应

我没有经常使用
XMLHttpRequests
,我确信,我在这里遗漏了一些基本的东西

现在我甚至不确定oAuth是否是实现我想要的目标的正确方法

任何帮助都将不胜感激

编辑:

同时,我做了更多的研究,发现()缺少了一些步骤来获取连接到GithubAPI的正确用户访问令牌

我将尝试完成这些步骤,如果可行,我将发布一个答案


如果有人知道如何准确地进行回购,仍然希望得到一个清晰的答案。

要获取回购问题列表,请调用以下API端点:

https://api.github.com/repos/${owner}/${repository}/问题

其中,
${owner}
是组织名称(不是您的github用户名),
${repo}
是您要列出问题的repo

此处的文档仅供参考:。然而,与许多OAuth端点文档一样,这是一个神秘的过程,因此需要数小时的修补

有一个实时工作的代码段,您可以查看示例代码,并对其进行测试和调整:

下面是代码片段的解释

它使用服务及其SDK(下面代码中的OAuth类),这不是必需的。然而,OAuth.io使您能够只使用前端(例如Javascript)实现OAuth流,因此实时工作代码片段可以自包含在JSFIDLE中,可以轻松地共享、调整、测试,甚至可以在您的网站上剪切和粘贴

$('#github-button').on('click', function() {

    // Initialize with your OAuth.io app public key
    OAuth.initialize('YOUR OAUTH.IO PUBLIC KEY');

    // Use popup for oauth
    OAuth.popup('github').then(provider => {

        const repository = 'YOUR REPOSITORY';

        const owner = 'REPOSITORY OWNER';

        provider.get(`/repos/${owner}/${repository}/issues`).then(data => {
            alert('Now you could see list of issues in the console!')
            console.log('Issue list:', data);
        })
    });
})
希望这有助于帮助您理解和测试端点,而无需花费数小时弄清楚文档,修改url和参数


注意:您需要访问回购协议,回购协议所有者需要授予您访问权限。有关屏幕截图的详细信息,请参阅原始文章:

可能问题在于用于创建URL的语法。这一行也有类似的问题-()@Poonacha使用与我的个人访问令牌相同的语法,所以我不认为这是问题所在