【问题标题】:PHP : generate a token using a Post methodPHP:使用 Post 方法生成令牌
【发布时间】:2017-03-16 17:37:17
【问题描述】:

早安,

在重新使用 Stackoverflow 之前,我整个下午都在谷歌上搜索,但没有真正成功。

我想做的是通过引用他们的doc 来从 myfox api 获取一个令牌,上面写着

必须生成新令牌才能执行 API 调用。这 可以通过调用以下方法请求token https://api.myfox.me/oauth2/token 并提供以下参数 (通过 POST):client_id、client_secret、用户名、密码和 grant_type 设置为密码。

因此我的代码:

function getToken()
{
$clientID = "a65000ee0c57f2e37260e90c375c3";
$clientSecret = "MyLongSecretCode";
$exportFile = "myfile.txt";
$userName = "somebody@somewhere.com";
$userPass = "myPassword123";
$sourceWebsite = "https://api.myfox.me/oauth2/token?client_id=" . $clientID .  "&client_secret=" . $clientSecret . "&username=" . $userName . "&password=" . $userPass . "&grant_type=password";

file_put_contents($exportFile, fopen($sourceWebsite , 'r'));
}

我得到的只是一个 PHP 错误,表明该方法是不允许的。

知道我在这里缺少什么吗?

非常感谢您在这个问题上提供的帮助。

编辑 17.03.2017 :

其他用户告诉我,我可以通过使用 curl 来实现这一点,而且看起来,通过阅读文档,我可以做到这一点:

必须生成新令牌才能执行 API 调用。这 可以通过调用以下方法请求token https://api.myfox.me/oauth2/token 并提供以下参数 (通过 POST):client_id、client_secret、用户名、密码和 grant_type 设置为密码。 curl -u CLIENT_ID:CLIENT_SECRET https://api.myfox.me/oauth2/token-d 'grant_type=password&username=YOUR_USERNAME&password=YOUR_PASSWORD' 或卷曲https://api.myfox.me/oauth2/token -d 'grant_type=password&client_id=CLIENT_ID&client_secret=CLIENT_SECRET&username=YOUR_USERNAME&password=YOUR_PASSWORD'

现在,对于我的问题:有没有办法将此 curl -u 查询转换为 php 指令并将内容输出到一个看起来像这样的文件:

{"access_token":"********************************","expires_in":3600,"token_type":"Bearer","scope":null,"refresh_token":"********************************"}

再次感谢您的帮助。

【问题讨论】:

  • file_put_contents 默认会执行GET 请求,而不是POST。您要么需要 create a stream context 并告诉它使用 post 并正确传递 post 数据,要么使用 curl 等其他工具,它可以更轻松地发布,但总体使用起来有点复杂。两者都会做同样的事情。
  • @JonathanKuhn:谢谢,我会看看这篇文章。很抱歉,如果它是重复的。您提供给我的示例中还有一个问题。在我的示例中,'var1' => 'some content',var1 将是'client_id','some content' 将是'a65000ee0c57f2e37260e90c375c3'。我说的对吗?
  • 没关系。虽然这不是一个确切的相关性,但该帖子应该回答这个问题,这就是我将其标记为重复的原因。我知道,如果您以前从未这样做过,可能会有点难以理解您需要搜索的内容。
  • 只是猜测,但您需要将数据作为发布数据发送。它不像您在这里所做的那样通过 URL 传递。在重复问题的顶部答案中,您可以看到他们正在创建一个名为 $postdata 的数组,他们在 $opts 数组下传递。它保存了发布的数据。您不会像在此处通过 https://URL/token?client_id=" . $clientID . "&client_secret=" . $clientSecret...etc 那样通过 URL 发送数据。像这样通过 URL 被认为对密钥/令牌或密码等秘密数据不利。

标签: php


【解决方案1】:

非常感谢您的提示,这是一个正在运行的脚本:

我希望这个脚本(和这篇文章)可以在某些时候对其他人有所帮助。祝你周末愉快。


<?php

$clientID = 'b85036758c385c3cd0c57f2e37260f91';
$clientSecret = 'MyLongSecretCode';
$username = 'myemailaddress@provider.net';
$passwd = 'mypassword';

// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'https://api.myfox.me/oauth2/token',
    CURLOPT_USERAGENT => 'Codular Sample cURL Request',
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array(
        'grant_type' => 'password',
        'client_id' => $clientID,
        'client_secret' => $clientSecret,
        'username' => $username,
        'password' => $passwd
    )
));
// Send the request & save response to $resp
$resp = curl_exec($curl);   

echo $resp;

// Close request to clear up some resources
curl_close($curl);

?>

【讨论】:

    猜你喜欢
    • 2011-09-14
    • 1970-01-01
    • 2014-01-21
    • 2018-01-15
    • 1970-01-01
    • 2019-11-11
    • 2017-06-22
    • 2013-12-19
    • 1970-01-01
    相关资源
    最近更新 更多