Example of API?

Hello.


I did not get to place an entry in his diary with the api.


Send GET request to api-yaru.yandex.ru/person/{uid}/post/ get information.


But if you are trying to add a record api-yaru.yandex.ru/person/{uid}/post/?oauth_token={token}&format=json&title=Title&content=Content, then get a 403 error FORBIDDEN (Required permission: "yaru:read"). Right on posting the application exposed. Tried to send requests via JavaScript, PHP, PhpStorm


Could give a working example, preferably in php, to embed messages in the diary or to send where you can dig?


PS

Attempt in PHP:
Method 1
public function post_on_wall($title, $message, $user_id){
 $token = json_decode(Tokens::get_token($user_id));

 $url = "https://api-yaru.yandex.ru/person/{$user_id}/post/";

 $data = array(
 'oauth_token' => $token->oauth_token,
 'format' => 'json',
 'title' => $title,
 'content' => $message
);

 $options = array(
 'http' => array(
 'header' => "Content-type: application/x-yaru-atom+json; type=entry\r\n",
 'method' => 'POST',
 'content' => http_build_query($data),
),
);
 $context = stream_context_create($options);
 $result = file_get_contents($url, false, $context);
var_dum($result);
}


error 401: UNAUTHORIZED

Method 2
public function post_on_wall($title, $message, $user_id){
 $token = json_decode(Tokens::get_token($user_id));

 $data = array(
 'oauth_token' => $token->oauth_token,
 'format' => $token->format,
 'title' => $title,
 'content' => $message
);

 $url = "https://api-yaru.yandex.ru/person/{$user_id}/post/";

 $curl = curl_init();
 curl_setopt($curl, CURLOPT_URL, $url.'?'.urldecode(http_build_query($data)));
 curl_setopt($curl, CURLOPT_POST, 1);
 curl_setopt($curl, CURLOPT_POSTFIELDS, urldecode(http_build_query($data)));
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($curl, THIS, false);
 $result = curl_exec($curl);
curl_close($curl);
var_dump($result);
}


Here at NULL

JS
$(document).ready(function(){
$.ajax({
 dataType: 'jsonp',
 type: 'POST',
 data: {'oauth_token': '{token}', 'format': 'json', 'title': 'Title', 'content': 'Content'},
 url: 'https://api-yaru.yandex.ru/person/{user_id}/post/',
 success: function(r) {console.dir(r);},
 error: function(e) {console.log(e);}
});
});


403: FORBIDDEN
October 3rd 19 at 02:43
2 answers
October 3rd 19 at 02:45
api-yaru.yandex.ru/person/{uid}/post/?oauth_token={token}&format=json&title=Title&content=Content

Right here you send a request? If Yes, then these options will come in the form of GET: $_GET['oauth_token']; and so forth, and these parameters should be sent by post on api-yaru.yandex.ru/person/{uid}/post/?format=json, that is, something like this:

$post_data = array(
'oauth_token' => 'oauth_token',
'title' => 'title',
'content' => 'content'
);

$result = post_request('api-yaru.yandex.ru/person/{uid}/post/?format=json', $post_data);

if ($result['status'] == 'ok'){

// Print headers 
echo $result['header']; 

echo '<hr>';

// print the result of the whole request:
echo $result['content'];

}
else {
 echo 'A error occured:' . $result['error']; 
}
Rather something like, Chet pozabavim PHP ;)

$url = 'http://server.com/path';
$data = array('key1' => 'value1', 'key2' => 'value2');

// use key 'http' even if you send the request to <a href="https://...">https://...</a>
$options = array(
 'http' => array(
 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
 'method' => 'POST',
 'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);

var_dump($result);
- chelsie_Kessler commented on October 3rd 19 at 02:48
Thank you for trying to help, but I have tried it, but your code was used, the result is the same. - erick.Lind commented on October 3rd 19 at 02:51
Check you have in the variable $token is written exactly the token here:

$token = json_decode(Tokens::get_token($user_id));
- chelsie_Kessler commented on October 3rd 19 at 02:54
What is recorded in the variable $token:
{
 'oauth_token' = {token},
 'format' = 'json'
}
- erick.Lind commented on October 3rd 19 at 02:57
In my opinion more options scheme and term must be passed to specify the type of record, here are the table types in the documentation api.yandex.ru/yaru/doc/ref/reference/post-types.xmbut I'm not sure.l - chelsie_Kessler commented on October 3rd 19 at 03:00
And you can chew the documentation?
Have done so:
$data = array(
 'oauth_token' => $token->oauth_token,
 'format' => $token->format,
 'scheme(a)' => 'text',
 'title' => $title,
 'content' => $message
);

The result is identical:( - erick.Lind commented on October 3rd 19 at 03:03
October 3rd 19 at 02:47
Hi I have code like this to work tried different had problems with the Russian language, of all the ways worked just like this
Here is a piece from the class to work with Arushi all orde did not, if there are more specific questions I will try to answer!
protected function _request()
{

 $params = array(
 'http' => array(
 'method' => $this->method,
 'header' => sprintf("Authorization: OAuth %s\r\n", $this->token).
 sprintf("Content-Type: %s\r\n", $this->contentType),
 'content' => $this->content
)
);

 $ctx = stream_context_create($params);

 $fp = fopen($this->url, 'r', false, $ctx);

 if (!$fp) {
 throw new Exception("Problem with $this->url, $php_errormsg");
}

 $response = @stream_get_contents($fp);

 if ($response === false) {
 throw new Exception("Problem reading data from $this->url, $php_errormsg");
}

 return $response;
}

Find more questions by tags Yandex