English 中文(简体)
页: 1
原标题:Twitter API - Failed to validate oauth signature and token PHP / CURL
  • 时间:2011-03-28 00:13:13
  •  标签:
  • php
  • curl

我花了过去几个小时的时间,试图改变所有类型的差异,但据Twitter AP说,这本来应该从第1步开始。

1 addition I have made to the script below is that I have added in: $header = array("Expect:");

在另一个问题中,我发现,从获得一个否认的问题到第100-continue的问题,这个问题有所帮助。

Issue: Failed to validate oauth signature and token is the response EVERY time!!!

我的员额数据实例:

Array ( [oauth_callback] => http://www.mysite.com//index.php [oauth_consumer_key] => hidden [oauth_nonce] => hidden [oauth_signature_method] => HMAC-SHA1 [oauth_timestamp] => 1301270847 [oauth_version] => 1.0 )

我的主人数据:

Array ( [0] => Expect: )

原文:

$consumer_key = "hidden";
$consumer_secret = "hidden";
function Post_Data($url,$data,$header){
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_POST, 1);  
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $result = curl_exec($ch);  
    curl_close($ch);
    return $result;
    }
$data[ oauth_callback ] = "http://".$_SERVER[ HTTP_HOST ].$_SERVER[ PHP_SELF ];
$data[ oauth_consumer_key ] = $consumer_key;
$data[ oauth_nonce ] = md5(time());
$data[ oauth_signature_method ] = "HMAC-SHA1";
$data[ oauth_timestamp ] = time();
$data[ oauth_version ] = "1.0";
$header = array("Expect:");
$content = Post_Data("http://api.twitter.com/oauth/request_token",$data,$header);
print_r($content);

没有人会看到我在这里可能犯下的明显错误? 最好是我不赞同其他人的守则,因为大多数例子都有完全的班级和整顿;我正在寻找最简单的办法!

最佳回答

Your problem is that you did not include the OAuth signature in your request.
You can read about the concept on this page.
A working implementation can be found here.

问题回答

I faced same issue, what I was missing is passing header in to the curl request. As shown in this question, I was also sending the $header = array( Expect: ), which was the problem in my case. I started sending signature in header with other data as below and it solved the case for me.

$header = calculateHeader($parameters,  https://api.twitter.com/oauth/request_token );

function calculateHeader(array $parameters, $url)
    {
        // redefine
        $url = (string) $url;

        // divide into parts
        $parts = parse_url($url);

        // init var
        $chunks = array();

        // process queries
        foreach($parameters as $key => $value) $chunks[] = str_replace( %25 ,  % , urlencode_rfc3986($key) .  ="  . urlencode_rfc3986($value) .  " );

        // build return
        $return =  Authorization: OAuth realm="  . $parts[ scheme ] .  ://  . $parts[ host ] . $parts[ path ] .  ",  ;
        $return .= implode( , , $chunks);

        // prepend name and OAuth part
        return $return;
    }

function urlencode_rfc3986($value)
    {
        if(is_array($value)) return array_map( urlencode_rfc3986 , $value);
        else
        {
            $search = array( + ,    ,  %7E ,  % );
            $replace = array( %20 ,  %20 ,  ~ ,  %25 );

            return str_replace($search, $replace, urlencode($value));
        }
    }




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签