繁体   English   中英

signature_invalid Yahoo oAuth

[英]signature_invalid Yahoo oAuth

我一直在试图解决这个问题,这似乎是一个普遍的问题,但是其他人正在做的事情似乎都没有用。

我正在使用Yahoo oAuth API(最终尝试导入联系人),但是一旦进入本教程的第4步( https://developer.yahoo.com/oauth/guide/oauth-accesstoken.html ),这就是该步骤我在尝试获取access_token的地方出现错误, array(1){[“ oauth_problem”] => string(17)“ signature_invalid”}

在getAccessToken()下的oauth_signature行上,我尝试用&,%26和%26&分隔,但没有一个起作用。 我尝试使用HMAC-SHA1代替&分隔的PLAINTEXT,但是所有这些都产生了相同的结果。 我还尝试了urlencode()处理所有内容,但没有效果。 当我var_dump(\\ Session :: get(\\ Auth :: user()-> id。'.oauth_token_secret'))时,我得到了看起来像真正的oauth_token_secret的东西,所以我不认为这是问题所在。

但是无论如何,这是代码(很抱歉,curl()方法到处都是)

<?php
namespace App\Models\oAuth2;

class Yahoo {
    static public $consumer_key     = 'xxx';
    static public $consumer_secret  = 'xxx';

    static public function getContactsLink() {
        parse_str(self::curl('https://api.login.yahoo.com/oauth/v2/get_request_token', 'post', NULL, [
            'oauth_consumer_key'        => self::$consumer_key,
            'oauth_signature'           => self::$consumer_secret . '&',
            'oauth_signature_method'    => 'PLAINTEXT',
            'oauth_callback'            => action('ImportController@yahooContacts'),
            'oauth_nonce'               => uniqid(rand()),
            'oauth_timestamp'           => time(),
            'oauth_version'             => '1.0',
            'xoauth_lang_pref'          => 'en-us'
        ]), $response);

        \Session::put(\Auth::user()->id . '.oauth_token_secret', $response['oauth_token_secret']);
        return $response['xoauth_request_auth_url'];
    }

    static public function getAccessToken() {
        parse_str(self::curl('https://api.login.yahoo.com/oauth/v2/get_token', 'post', NULL, [
            'oauth_consumer_key'        => self::$consumer_key,
            'oauth_signature'           => self::$consumer_secret . '%26' . \Session::pull(\Auth::user()->id . '.oauth_token_secret'),
            'oauth_signature_method'    => 'PLAINTEXT',
            'oauth_nonce'               => uniqid(rand()),
            'oauth_timestamp'           => time(),
            'oauth_version'             => '1.0',
            'oauth_verifier'            => \Input::get('oauth_verifier'),
            'oauth_token'               => \Input::get('oauth_token')
        ]), $response);

        dd($response);
    }

    static function curl($url, $method = 'get', $header = null, $postdata = null, $includeheader=FALSE, $timeout = 60) {
        $s = curl_init();
        curl_setopt($s,CURLOPT_URL, $url);
        if ($header)
            curl_setopt($s,CURLOPT_HTTPHEADER, $header);
        /*if ($this->debug)*/
        curl_setopt($s,CURLOPT_VERBOSE, FALSE);
            curl_setopt($s,CURLOPT_TIMEOUT, $timeout);
        curl_setopt($s,CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($s,CURLOPT_MAXREDIRS, 3);
        curl_setopt($s,CURLOPT_RETURNTRANSFER, true);
        curl_setopt($s,CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($s,CURLOPT_COOKIEJAR, 'cookie.txt');
        curl_setopt($s,CURLOPT_COOKIEFILE, 'cookie.txt');
        if(strtolower($method) == 'post')
        {
            curl_setopt($s,CURLOPT_POST, true);
            curl_setopt($s,CURLOPT_POSTFIELDS, $postdata);
        }
        else if(strtolower($method) == 'delete')
        {
            curl_setopt($s,CURLOPT_CUSTOMREQUEST, 'DELETE');
        }
        else if(strtolower($method) == 'put')
        {
            curl_setopt($s,CURLOPT_CUSTOMREQUEST, 'PUT');
            curl_setopt($s,CURLOPT_POSTFIELDS, $postdata);
        }
        curl_setopt($s,CURLOPT_HEADER, $includeheader);
        //curl_setopt($s,CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1');
        curl_setopt($s, CURLOPT_SSL_VERIFYPEER, false);

        $html    = curl_exec($s);
        $status = curl_getinfo($s, CURLINFO_HTTP_CODE);

        curl_close($s);
        return $html;
    }
}

对于其他遇到此问题的人,我想出了我的问题。 get_token调用必须显式为GET而不是POST(即使文档说是GET&POST)。

这是我更新的getAccessToken()方法的样子:

static public function getAccessToken() {
    parse_str(self::curl(
        'https://api.login.yahoo.com/oauth/v2/get_token?oauth_consumer_key=' . self::$consumer_key .'&oauth_signature=' . self::$consumer_secret . '%26' . \Session::pull(\Auth::user()->id . '.oauth_token_secret') . '&oauth_signature_method=PLAINTEXT&oauth_nonce=' . uniqid(rand()) . '&oauth_timestamp=' . time() . '&oauth_version=1.0&oauth_verifier=' . \Input::get('oauth_verifier') . '&oauth_token=' . \Input::get('oauth_token')
    ), $response);

    dd($response);
}

而且效果很好。

暂无
暂无

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

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