在php中使用oauth2访问twitter,将其移动到Google应用程序引擎

在php中使用oauth2访问twitter,将其移动到Google应用程序引擎,php,google-app-engine,twitter,localhost,twitter-oauth,Php,Google App Engine,Twitter,Localhost,Twitter Oauth,我在网上找到了下面的代码()并尝试在Google App Engine上实现它 当我在本地主机上运行它时,它工作得非常好,但当我将它部署到AppEngine时,$encodedData中没有任何内容 $authContext = stream_context_create(array( 'http' => array( 'method' => 'POST', 'header' => "Authorization: Basic " .

我在网上找到了下面的代码()并尝试在Google App Engine上实现它

当我在本地主机上运行它时,它工作得非常好,但当我将它部署到AppEngine时,
$encodedData
中没有任何内容

$authContext = stream_context_create(array(
    'http' => array(
        'method'  => 'POST',
        'header'  => "Authorization: Basic " . base64_encode(($consumerKey).':'.($consumerSecret)) . "\r\n".
                "Content-type: application/x-www-form-urlencoded;charset=UTF-8\r\n".
                "Content-Length: 29\r\n".
                "\r\n".
                "grant_type=client_credentials",
    ),
));
$authResponse = file_get_contents("https://api.twitter.com/oauth2/token", false, $authContext);
$decodedAuth = json_decode($authResponse, true);
$bearerToken = $decodedAuth["access_token"];

$context = stream_context_create(array(
    'http' => array(
        'method'  => 'GET',
        'header'  => "Authorization: Bearer " . $bearerToken . "\r\n".
                     "\r\n".
                     "grant_type=client_credentials",
    ),
));

$encodedData = file_get_contents('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=samkiesupdates&count='."100", false, $context);
谁知道我需要在代码中修改什么才能让它在谷歌应用引擎服务器上工作?
也许在我上面链接的论坛帖子的回复中说了,但遗憾的是我不会说德语,所以我说不出来。

你应该将帖子正文放在选项的“内容”字段中,不需要指定内容长度

$authContext = stream_context_create(array(
    'http' => array(
        'method'  => 'POST',
        'header'  => "Authorization: Basic ".base64_encode(($consumerKey).':'.($consumerSecret))."\r\n".
                "Content-type: application/x-www-form-urlencoded;charset=UTF-8\r\n",
        'content' => 'grant_type=client_credentials',
    ),
));

对您发出的另一个http请求也这样做。

只是为了澄清这些内容,并使整个问题/答案更加清晰可见。以下是工作代码示例:

  <?php

    $consumerKey    = <YOUR CONSUMER KEY>;

    $consumerSecret = <YOUR CONSUMER SECRET>;

    $authContext = stream_context_create(array(

      'http' => array(

        'method'  => 'POST',

        'header'  => "Authorization: Basic " . base64_encode(($consumerKey).':'.($consumerSecret)) . "\r\n".

          "Content-type: application/x-www-form-urlencoded;charset=UTF-8\r\n",

        'content' => "grant_type=client_credentials"

        ),

    ));

    $authResponse = file_get_contents("https://api.twitter.com/oauth2/token", false, $authContext);

    $decodedAuth = json_decode($authResponse, true);

    $bearerToken = $decodedAuth["access_token"];

    $context = stream_context_create(array(

      'http' => array(

          'method'  => 'GET',

          'header'  => "Authorization: Bearer " . $bearerToken . "\r\n",

          'content' => "grant_type=client_credentials"

        ),

    ));

    $encodedData = file_get_contents('https://api.twitter.com/1.1/statuses/show.json?id=<YOUR TWEET ID>', false, $context);


    $result = json_decode($encodedData);

您将帖子正文放在上下文选项的标题字段中,这对我来说似乎是非常错误的。可能有很多错误,我正在使用其他人的代码,并且我还不熟悉使用http请求,正在尝试学习。我试过了,它在GAE上产生了此错误(在本地主机上工作正常):警告:file\u get\u contents():无法打开流:HTTP请求失败!HTTP/1.0 400 main.php中的错误请求第35行抱歉,我的错误,它在本地主机上甚至不起作用,所以根本不起作用。或者,如果我只更改了第一个请求,代码就可以工作。但是,当我尝试对第二个请求进行相同的更改时,我会产生上面的错误。我将尝试将其部署到GAE,看看这是否足够。编辑它只改变了第一个请求,谢谢你斯图尔特!