# 【#はてなAPI認証】【#不完全なコード】このコードは機能しません．

おはようございます．久々にAPI認証で躓いています．この頃は躓いたことがなかったのですがはてなAPI認証で躓いております．エラー内容があまりにもアバウト過ぎて何処の項目でエラーになっているのかがわからない感じです．分かった方はコメント欄にコメント頂けたらと思っています．宜しくお願い致します．

oauthSignatureを作っているところでコケているぽっいと思っているのですが、それが正しいのかどうかも定かではないです．近日中にcurlから参考にしているような方法に変えてみようと思っています．

参考にしたサイトは[Qiita](https://qiita.com/taoka-toshiaki/questions/8c505c035bf728f9cd2b)の質問に記載していますので良かったら覗いてみてください．

```
<?php
ini_set('display_errors', 1);
require '../config/config.php';

class hatena
{
    public $oauthCallback = OAUTH_CALLBACK;
    public $oauthConsumerKey = OAUTH_CONSUMER_KEY;
    public $oauthConsumeSecret = OAUTH_CONSUMER_SECRET;
    public $oauthNonce = '';
    public $oauthSignature = null;
    public $oauthSignatureMethod = "HMAC-SHA1";
    public $oauthTimestamp = '';
    public $oauthVersion = "1.0";
    public $contentType = 'application/x-www-form-urlencoded';
    public $oauthParameters = [];

    public function oauthInitiate()
    {

        $url = 'https://www.hatena.com/oauth/initiate';
        $this->oauthNonce = uniqid();
        $this->oauthTimestamp = time();

        $this->oauthParameters = [
            'oauth_consumer_key' => rawurlencode($this->oauthConsumerKey),
            'oauth_nonce' => rawurlencode($this->oauthNonce),
            'oauth_signature_method' => rawurlencode($this->oauthSignatureMethod),
            'oauth_timestamp' => rawurlencode($this->oauthTimestamp),
        ];


        $params = [
            'scope' => 'read_public,write_public,read_private,write_private'
        ];

        $this->getSignature($url, 'POST', $params);
        $this->oauthParameters['oauth_signature'] = rawurlencode($this->oauthSignature);

        $ch = curl_init($url);

        $headers = [ //'.$this->oauthParameters['realm'].'
            'Authorization: OAuth realm="",oauth_callback="' .  rawurlencode($this->oauthCallback) . '",oauth_consumer_key="' . $this->oauthParameters['oauth_consumer_key'] . '",oauth_nonce="' . $this->oauthParameters['oauth_nonce'] . '",oauth_signature="' . $this->oauthParameters['oauth_signature'] . '",oauth_signature_method="' . $this->oauthParameters['oauth_signature_method'] . '",oauth_timestamp="' . $this->oauthParameters['oauth_timestamp'] . '",oauth_version="1.0"',
            'Content-Type: ' . $this->contentType,
            'Content-Length: ' . (string)strlen($this->contentType)
        ];


        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $response = curl_exec($ch);
        if (curl_error($ch)) {
            echo 'Curl error: ' . curl_error($ch);
        }
        parse_str($response, $response_params);
        var_dump($response_params);
        curl_close($ch);

        return $this;
    }

    public function getSignature($url, $method = 'POST', $params = [], $oauthTokenSecret = '')
    {

        foreach($params as $key=>$value){
            $params[$key] = rawurlencode($value);
        }
        $hasBase = http_build_query($this->oauthsort(array_merge($this->oauthParameters, $params)), '', '&', PHP_QUERY_RFC3986);

        $signingKey = implode('&', [rawurlencode($this->oauthConsumeSecret), rawurlencode($oauthTokenSecret)]);
        $baseString = implode('&', [
            rawurlencode($method),
            rawurlencode($url),
            $hasBase,
        ]);

        $signature = hash_hmac('sha1', $baseString, $signingKey, true);
        $signature = base64_encode($signature);
        $this->oauthSignature = $signature;

        return $this;
    }
    //OAuth式 パラメータのソート関数
    public function oauthsort($a)
    {
        $b = array_map(null, array_keys($a), $a);
        usort($b, ['hatena', 'oauthcmp']);
        $c = array();
        foreach ($b as $v) {
            $c[$v[0]] = $v[1];
        }

        return $c;
    }
    public function oauthcmp($a, $b)
    {
        return strcmp($a[0], $b[0])
            ? strcmp(rawurlencode($a[0]), rawurlencode($b[0]))
            : strcmp(rawurlencode($a[1]), rawurlencode($b[1]));
    }
}

(new hatena)->oauthInitiate();


```



こちらでも解決策を模索してみます．解決出来れば追記したいと思っています．

追記：：解決出来ました．

> [【PHP言語】はてなAPIを使用してURLをはてなブックマークに保存するコード](https://zip358.com/2024/07/07/%e3%80%90php%e8%a8%80%e8%aa%9e%e3%80%91%e3%81%af%e3%81%a6%e3%81%aaapi%e3%82%92%e4%bd%bf%e7%94%a8%e3%81%97%e3%81%a6url%e3%82%92%e3%81%af%e3%81%a6%e3%81%aa%e3%83%96%e3%83%83%e3%82%af%e3%83%9e%e3%83%bc.html)



明日へ続く．

 [ ![aerial view of a city and island](https://zip358.com/wp-content/uploads/2022/04/pexels-photo-3566139-300x300.jpeg) 観ずになりそうな映画、その壱その弐

 ](https://zip358.com/2025/11/09/%e8%a6%b3%e3%81%9a%e3%81%ab%e3%81%aa%e3%82%8a%e3%81%9d%e3%81%86%e3%81%aa%e6%98%a0%e7%94%bb%e3%80%81%e3%81%9d%e3%81%ae%e5%a3%b1%e3%81%9d%e3%81%ae%e5%bc%90.html) 

 [ ![low angle photography of cherry blossoms](https://zip358.com/wp-content/uploads/2022/09/pexels-photo-2058853-300x300.jpeg) 求職者マイページホーム（ハローワーク）のバーコードは何の種類を使用しているの？

 ](https://zip358.com/2025/11/08/%e6%b1%82%e8%81%b7%e8%80%85%e3%83%9e%e3%82%a4%e3%83%9a%e3%83%bc%e3%82%b8%e3%83%9b%e3%83%bc%e3%83%a0%ef%bc%88%e3%83%8f%e3%83%ad%e3%83%bc%e3%83%af%e3%83%bc%e3%82%af%ef%bc%89%e3%81%ae%e3%83%90%e3%83%bc.html) 

 [ ![](https://zip358.com/wp-content/uploads/2025/05/image-2.png) みんな働いているから偉いなとかお金の話とか人生とか馬鹿みたいに考えた

 ](https://zip358.com/2025/11/07/%e3%81%bf%e3%82%93%e3%81%aa%e5%83%8d%e3%81%84%e3%81%a6%e3%81%84%e3%82%8b%e3%81%8b%e3%82%89%e5%81%89%e3%81%84%e3%81%aa%e3%81%a8%e3%81%8b%e3%81%8a%e9%87%91%e3%81%ae%e8%a9%b1%e3%81%a8%e3%81%8b%e4%ba%ba.html) 

 [ ![](https://zip358.com/wp-content/uploads/2025/11/so-so.png) so-soさんが良い感じの曲を作り出しています、それと日常.

 ](https://zip358.com/2025/11/06/so-so%e3%81%95%e3%82%93%e3%81%8c%e8%89%af%e3%81%84%e6%84%9f%e3%81%98%e3%81%ae%e6%9b%b2%e3%82%92%e4%bd%9c%e3%82%8a%e5%87%ba%e3%81%97%e3%81%a6%e3%81%84%e3%81%be%e3%81%99%e3%80%81%e3%81%9d%e3%82%8c.html) 

 [ ![person with body painting](https://zip358.com/wp-content/uploads/2022/07/pexels-photo-1209843-300x300.jpeg) AIに仕事奪われたって記事はGeminiの生成文、いや分からないって.

 ](https://zip358.com/2025/11/05/ai%e3%81%ab%e4%bb%95%e4%ba%8b%e5%a5%aa%e3%82%8f%e3%82%8c%e3%81%9f%e3%81%a3%e3%81%a6%e8%a8%98%e4%ba%8b%e3%81%afgemini%e3%81%ae%e7%94%9f%e6%88%90%e6%96%87%e3%80%81%e3%81%84%e3%82%84%e5%88%86%e3%81%8b.html) 

 

[](https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fzip358.com%2F2024%2F07%2F03%2F%25e3%2580%2590%25e3%2581%25af%25e3%2581%25a6%25e3%2581%25aaapi%25e8%25aa%258d%25e8%25a8%25bc%25e3%2580%2591%25e3%2580%2590%25e4%25b8%258d%25e5%25ae%258c%25e5%2585%25a8%25e3%2581%25aa%25e3%2582%25b3%25e3%2583%25bc%25e3%2583%2589%25e3%2580%2591%25e3%2581%2593%25e3%2581%25ae%25e3%2582%25b3%25e3%2583%25bc%25e3%2583%2589.html&linkname=%E3%80%90%23%E3%81%AF%E3%81%A6%E3%81%AAAPI%E8%AA%8D%E8%A8%BC%E3%80%91%E3%80%90%23%E4%B8%8D%E5%AE%8C%E5%85%A8%E3%81%AA%E3%82%B3%E3%83%BC%E3%83%89%E3%80%91%E3%81%93%E3%81%AE%E3%82%B3%E3%83%BC%E3%83%89%E3%81%AF%E6%A9%9F%E8%83%BD%E3%81%97%E3%81%BE%E3%81%9B%E3%82%93%EF%BC%8E "Facebook")[](https://www.addtoany.com/add_to/x?linkurl=https%3A%2F%2Fzip358.com%2F2024%2F07%2F03%2F%25e3%2580%2590%25e3%2581%25af%25e3%2581%25a6%25e3%2581%25aaapi%25e8%25aa%258d%25e8%25a8%25bc%25e3%2580%2591%25e3%2580%2590%25e4%25b8%258d%25e5%25ae%258c%25e5%2585%25a8%25e3%2581%25aa%25e3%2582%25b3%25e3%2583%25bc%25e3%2583%2589%25e3%2580%2591%25e3%2581%2593%25e3%2581%25ae%25e3%2582%25b3%25e3%2583%25bc%25e3%2583%2589.html&linkname=%E3%80%90%23%E3%81%AF%E3%81%A6%E3%81%AAAPI%E8%AA%8D%E8%A8%BC%E3%80%91%E3%80%90%23%E4%B8%8D%E5%AE%8C%E5%85%A8%E3%81%AA%E3%82%B3%E3%83%BC%E3%83%89%E3%80%91%E3%81%93%E3%81%AE%E3%82%B3%E3%83%BC%E3%83%89%E3%81%AF%E6%A9%9F%E8%83%BD%E3%81%97%E3%81%BE%E3%81%9B%E3%82%93%EF%BC%8E "X")[](https://www.addtoany.com/add_to/bluesky?linkurl=https%3A%2F%2Fzip358.com%2F2024%2F07%2F03%2F%25e3%2580%2590%25e3%2581%25af%25e3%2581%25a6%25e3%2581%25aaapi%25e8%25aa%258d%25e8%25a8%25bc%25e3%2580%2591%25e3%2580%2590%25e4%25b8%258d%25e5%25ae%258c%25e5%2585%25a8%25e3%2581%25aa%25e3%2582%25b3%25e3%2583%25bc%25e3%2583%2589%25e3%2580%2591%25e3%2581%2593%25e3%2581%25ae%25e3%2582%25b3%25e3%2583%25bc%25e3%2583%2589.html&linkname=%E3%80%90%23%E3%81%AF%E3%81%A6%E3%81%AAAPI%E8%AA%8D%E8%A8%BC%E3%80%91%E3%80%90%23%E4%B8%8D%E5%AE%8C%E5%85%A8%E3%81%AA%E3%82%B3%E3%83%BC%E3%83%89%E3%80%91%E3%81%93%E3%81%AE%E3%82%B3%E3%83%BC%E3%83%89%E3%81%AF%E6%A9%9F%E8%83%BD%E3%81%97%E3%81%BE%E3%81%9B%E3%82%93%EF%BC%8E "Bluesky")[](https://www.addtoany.com/add_to/line?linkurl=https%3A%2F%2Fzip358.com%2F2024%2F07%2F03%2F%25e3%2580%2590%25e3%2581%25af%25e3%2581%25a6%25e3%2581%25aaapi%25e8%25aa%258d%25e8%25a8%25bc%25e3%2580%2591%25e3%2580%2590%25e4%25b8%258d%25e5%25ae%258c%25e5%2585%25a8%25e3%2581%25aa%25e3%2582%25b3%25e3%2583%25bc%25e3%2583%2589%25e3%2580%2591%25e3%2581%2593%25e3%2581%25ae%25e3%2582%25b3%25e3%2583%25bc%25e3%2583%2589.html&linkname=%E3%80%90%23%E3%81%AF%E3%81%A6%E3%81%AAAPI%E8%AA%8D%E8%A8%BC%E3%80%91%E3%80%90%23%E4%B8%8D%E5%AE%8C%E5%85%A8%E3%81%AA%E3%82%B3%E3%83%BC%E3%83%89%E3%80%91%E3%81%93%E3%81%AE%E3%82%B3%E3%83%BC%E3%83%89%E3%81%AF%E6%A9%9F%E8%83%BD%E3%81%97%E3%81%BE%E3%81%9B%E3%82%93%EF%BC%8E "Line")[](https://www.addtoany.com/add_to/threads?linkurl=https%3A%2F%2Fzip358.com%2F2024%2F07%2F03%2F%25e3%2580%2590%25e3%2581%25af%25e3%2581%25a6%25e3%2581%25aaapi%25e8%25aa%258d%25e8%25a8%25bc%25e3%2580%2591%25e3%2580%2590%25e4%25b8%258d%25e5%25ae%258c%25e5%2585%25a8%25e3%2581%25aa%25e3%2582%25b3%25e3%2583%25bc%25e3%2583%2589%25e3%2580%2591%25e3%2581%2593%25e3%2581%25ae%25e3%2582%25b3%25e3%2583%25bc%25e3%2583%2589.html&linkname=%E3%80%90%23%E3%81%AF%E3%81%A6%E3%81%AAAPI%E8%AA%8D%E8%A8%BC%E3%80%91%E3%80%90%23%E4%B8%8D%E5%AE%8C%E5%85%A8%E3%81%AA%E3%82%B3%E3%83%BC%E3%83%89%E3%80%91%E3%81%93%E3%81%AE%E3%82%B3%E3%83%BC%E3%83%89%E3%81%AF%E6%A9%9F%E8%83%BD%E3%81%97%E3%81%BE%E3%81%9B%E3%82%93%EF%BC%8E "Threads")[](https://www.addtoany.com/add_to/copy_link?linkurl=https%3A%2F%2Fzip358.com%2F2024%2F07%2F03%2F%25e3%2580%2590%25e3%2581%25af%25e3%2581%25a6%25e3%2581%25aaapi%25e8%25aa%258d%25e8%25a8%25bc%25e3%2580%2591%25e3%2580%2590%25e4%25b8%258d%25e5%25ae%258c%25e5%2585%25a8%25e3%2581%25aa%25e3%2582%25b3%25e3%2583%25bc%25e3%2583%2589%25e3%2580%2591%25e3%2581%2593%25e3%2581%25ae%25e3%2582%25b3%25e3%2583%25bc%25e3%2583%2589.html&linkname=%E3%80%90%23%E3%81%AF%E3%81%A6%E3%81%AAAPI%E8%AA%8D%E8%A8%BC%E3%80%91%E3%80%90%23%E4%B8%8D%E5%AE%8C%E5%85%A8%E3%81%AA%E3%82%B3%E3%83%BC%E3%83%89%E3%80%91%E3%81%93%E3%81%AE%E3%82%B3%E3%83%BC%E3%83%89%E3%81%AF%E6%A9%9F%E8%83%BD%E3%81%97%E3%81%BE%E3%81%9B%E3%82%93%EF%BC%8E "Copy Link")[](https://www.addtoany.com/share)
