简体   繁体   English

403当我发送POST请求时被禁止

[英]403 Forbidden when I send POST request

Good evening, I wanted to connect to the https://accounts.nintendo.com/ site via twitter with cURL, so I first made a GET request on the nintendo site connection page to get the csrf_token and then a POST request to the action url of the nintendo site form in order to connect via twitter. 晚上好,我想使用cURL通过twitter连接到https://accounts.nintendo.com/网站,因此我首先在nintendo网站连接页面上进行了GET请求,以获取csrf_token,然后进行POST请求任天堂站点表单的URL,以便通过Twitter连接。

Normally when everything is going well the answer request should send me back: 302 Found then the oAuth link to connect me via twitter. 通常,当一切进展顺利时,答案请求应将我发送回去:302找到了,然后通过oAuth链接通过Twitter与我联系。

I tried of modify the userAgent and Header 我试图修改userAgent和Header

<?php
include('simple_html_dom.php');

// SHOW THE PAGE TO GET CSRF TOKEN
$ch = curl_init();

$url = 'https://accounts.nintendo.com/login';



curl_setopt_array($ch,
  array(
     CURLOPT_URL => $url,

     CURLOPT_CAINFO => dirname(__FILE__)."/cacert.pem",
     CURLOPT_COOKIESESSION => false,
     CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36",
     CURLOPT_HTTPHEADER => array(
       'Host: accounts.nintendo.com',
       'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
       'Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3',
       'Pragma: ',

       'Connection: keep-alive',

     ),
     CURLINFO_HEADER_OUT => true,
     CURLOPT_COOKIEFILE => dirname(__FILE__) . "/cookies.txt",
     CURLOPT_COOKIEJAR => dirname(__FILE__) . "/cookies.txt",
     CURLOPT_FRESH_CONNECT => false,


    // The data to transfer with the response.

    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_RETURNTRANSFER => true,


  )
);


$data = curl_exec($ch);


// PARSER THE TOKEN CSRF
$dom = new simple_html_dom();

$html = $dom->load($data);


foreach($html->find('li[class="LoginForm_snsButton LoginForm_snsButton-row LoginForm_snsButton-twitter"] input[name="csrf_token"]') as $element)
{
  $csrf_token = $element->value.'<br/>';

}

curl_close($ch);

//SEND POST REQUEST

$ch2 = curl_init();

$url = 'https://accounts.nintendo.com/federation/twitter';


$postData = array(

  "post_federation_redirect_uri" => "https://accounts.nintendo.com/",
  "post_federation_reauthenticate" => "",
  "redirect_after" => "5",
  "csrf_token" => $csrf_token,
  "display" => ""
);
curl_setopt_array($ch2,
  array(
     CURLOPT_URL => $url,

     CURLOPT_CAINFO => dirname(__FILE__)."/cacert.pem",
     CURLOPT_COOKIESESSION => false,
     CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0",


     CURLOPT_COOKIEFILE => dirname(__FILE__) . "/cookies.txt",
     CURLOPT_COOKIEJAR => dirname(__FILE__) . "/cookies.txt",
     CURLOPT_FRESH_CONNECT => false,
     CURLOPT_CUSTOMREQUEST => 'POST',
     CURLOPT_POSTFIELDS => http_build_query($postData),
     CURLINFO_HEADER_OUT => true,
    // The data to transfer with the response.
    CURLOPT_HEADER => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_RETURNTRANSFER => true,


  )
);

$exec = curl_exec($ch2);
$header = curl_getinfo($ch2);
echo $header['request_header'].'</br></br></br>';

echo $header['header_size'];

echo $exec;
curl_close($ch2);


?>

the problem is that I get a nice 403 Forbidden (I don't know if it's because I'm making the request from localhost), here's a screen showing the request header, the response header and the content of the site's response: http://www.noelshack.com/2019-15-6-1555110358-screenshot-1.jpg 问题是我得到了一个很好的403 Forbidden(我不知道是否是因为我正在从本地主机发出请求),这是一个显示请求标头,响应标头和站点响应内容的屏幕: http: //www.noelshack.com/2019-15-6-1555110358-screenshot-1.jpg

What is your chmod permission for simple_html_dom.php and for your script ? 您对simple_html_dom.php和脚本的chmod权限是什么?

If you did not put permission 644 try this : 如果您没有授予权限644,请尝试以下操作:

Chmod both files with $chmod 644 YourScript.php 使用$chmod 644 YourScript.php两个文件的$chmod 644 YourScript.php

And, in a second case reload your script on your Web-Browser and wait for error 403 and look on your Log File. 并且,在第二种情况下,将脚本重新加载到Web浏览器上,等待错误403,然后查看日志文件。

  • In case you use Apache2 look at /var/log/apache2/error.log 如果您使用Apache2,请查看/var/log/apache2/error.log
  • Otherwise or if you use Nginx go in /var/log/nginx/error.log 否则,或者如果您使用Nginx,请进入/var/log/nginx/error.log

Give me your results so I could help you a little better. 给我您的结果,以便我可以帮助您更好。 ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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