How to generate the correct signature (SIG) in the OK.RU API. We work with error 104

Not so long ago I ran into difficulty. I couldn’t get the correct SIG (error 104) when I asked for the API in OK.RU (classmates). As it turned out, I wasn’t the only one, the recipes offered on the network didn’t work very well (I checked many using the poke method). Actually they worked, I just didn’t understand how to build the right query correctly, so that SIG was also correct.



Specifically, this post is dedicated to exactly 104 OK.RU API error.



PARAM_SIGNATURE 104  
      
      





Below is a simple code to get a list of albums. The code is a little crutch (I wrote the right class for myself, but it is specific and not very useful to anyone), but it describes the essence, and many novice programmers can create their own class based on an example.



 protected $params = array( 'application_id' => '5*******6', 'application_key' => 'C***********A', 'session_secret_key' => '*************0' ); protected $apiUrl = 'https://api.ok.ru/fb.do?'; public function getalbums( Request $request ) { //access_token        . $userok = Userok::where('useroks.user_id', '=', $request->user_id)->first(); $paramsArray = array( 'application_key' => $this->params['application_key'], 'format' => 'json', 'method' => 'photos.getAlbums', ); ksort($paramsArray); foreach($paramsArray as $k=>$v) { $paramsStr .= $k . "=" . $v; } /***    .   ,     (    ). $sig = strtolower( md5( $paramsStr) . md5( $userok->access_token .$this->params['session_secret_key'] ) ); **/ $sectret_key = md5($userok->access_token.$this->params['session_secret_key']); $sig = md5($paramsStr.$sectret_key); $paramsArray['sig'] = $sig; $paramsArray['access_token'] = $userok->access_token; return $this->sendRequest( $this->apiUrl, $paramsArray, 'POST' ); } protected function sendRequest($url = '', $params = array(), $method = 'POST') { if(is_array($params)) { $params = http_build_query($params); } $ch = curl_init(); if($method == 'GET') { $url .= $params; } else if($method == 'POST') { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $result = curl_exec($ch); curl_close($ch); return json_decode($result, true); }
      
      





But for this to work, you need the correct $ paramsArray, otherwise you will solve the 104th error, but get another. Many beginning programmers will probably ignore the efforts of OK.RU to help developers.



So, follow the link https://apiok.ru/dev/methods/ .



We find this form:







We fill in the necessary data and get essentially a ready-made instruction, and in addition we test our request.



Hope the article was helpful.



All Articles