使用php-github-api进行Github身份验证

我正在尝试使用 php-github-api 由KnpLabs提供。 我继续在我的Github帐户中创建了一个应用程序,以获取client_id和密钥。 </ p>

问题是,我无法使用此库进行身份验证。 返回null结果。 它的实现看起来很简单,但我不能让它工作。 查看我如何实现它。</ p>

  try {
$ client = new Github \ Client();

$ auth = $ client-&gt; authenticate(' myclientid','mysecret',AUTH_URL_CLIENT_ID);

$ email = $ client-&gt; api('current_user') - &gt; emails() - &gt; all();

返回Response :: json (array(“user”=&gt; $ emails));
} catch(异常$ e){
返回Response :: json(array('failed',$ e-&gt; getMessage())); \ n}
</ code> </ pre>

这是我从上面得到的结果:</ p>

  [“failed”,“Requires 认证“] 
</ code> </ pre>

请有人帮我弄清楚我做错了什么。</ p>

谢谢。</ p>

</ div>

展开原文

原文

I am trying to implement Github authentication in my application (built using Laravel 4) using php-github-api by KnpLabs. I went ahead and created an app in my Github account to obtain the client_id and the secret key.

The problem is, I cannot authenticate using this library. A null result is returned. It implementation seems simple but I cannot get it work. Checkout how I am implementing it.

try{
        $client = new Github\Client();

        $auth= $client->authenticate('myclientid','mysecret',AUTH_URL_CLIENT_ID);

        $emails = $client->api('current_user')->emails()->all();

        return Response::json(array("user"=>$emails));
    }catch(Exception $e){
        return Response::json(array('failed',$e->getMessage()));
    }

This is the result I get from the above:

 ["failed","Requires authentication"]

Please someone help me figure out what I am doing wrong.

Thank you.

dsaaqdz6223
dsaaqdz6223 这是一个无耻的插件,但它可以帮助您查看客户端使用Runscope来回发送的内容。创建一个免费帐户,记下您的存储桶密钥,然后在创建客户端后添加此行:$client.setOption(“base_url”,“api-github-com-yourbucketkey.runscope.net”);.然后再次提出请求并在您的Runscope帐户中查看。然后,您可以看到生成的实际HTTP请求/响应以及Github返回的错误。
接近 7 年之前 回复

2个回答



我相信
AUTH_URL_CLIENT_ID </ code>
应该是
Github \ Client :: AUTH_URL_CLIENT_ID </ 代码> </ p>
</ div>

展开原文

原文

I believe AUTH_URL_CLIENT_ID should be Github\Client::AUTH_URL_CLIENT_ID



这更像是一个Github问题,而不是一个php-github-api问题,真的:</ p>

< pre> curl“https://api.github.com/user/emails?client_id=xxxx&amp;client_secret=yyyy"

nn

也会失败, 而:</ p>

  curl“https://api.github.com/user/emails”-u mbontemps 
</ code> </ pre>

< p>工作正常。</ p>

我认为原因是client_id和client_secret对应用程序</ strong>进行身份验证,而第二个命令对用户进行身份验证</ strong >。</ p>

这就像你在问:“嘿,申请,告诉我你的电子邮件”</ em>。

该应用正在回答:“等等,什么?我不是用户,伙计!”</ em> </ p>

因此IMO您需要为您的用户授权应用程序(这会给您一个令牌) )然后使用此令牌通过OAuth机制进行身份验证。</ p>

cf。 http://developer.github.com/v3/#basic-authentication </ p>

祝你好运!</ p>
</ div>

展开原文

原文

This is more of a Github matter rather than a php-github-api question, really:

curl "https://api.github.com/user/emails?client_id=xxxx&client_secret=yyyy"

will fail too, whereas:

curl "https://api.github.com/user/emails" -u mbontemps

works just fine.

I think the reason is that client_id and client_secret authenticate an application, while the second command authenticates a user.

It's like you're asking: "hey, application, tell me your emails".
And the app is answering: "wait, what? I'm no user, dude!"

So IMO you'll need to authorize the application for your user (which will give you a token) and then use this token to authenticate with the OAuth mechanism.

cf. http://developer.github.com/v3/#basic-authentication

Good luck!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问