M
M
Maxim2013-06-25 11:55:27
Yandex
Maxim, 2013-06-25 11:55:27

API example?

Hello.
I can't seem to get a post in my diary using api.
I send a GET request to api-yaru.yandex.ru/person/ {uid}/post/ and get information.
But if I try to add an entry api-yaru.yandex.ru/person/ {uid}/post/?oauth_token={token}&format=json&title=Title&content=Content, I get a 403 FORBIDDEN error (Required permission: "yaru:read") . The rights to posting at the application have been set. Tried sending requests via JavaScript, PHP, PhpStorm
Could you provide a working example, preferably in php, for posting blog posts or pointing me where I can dig?
PS
Trying 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, CURLOPT_SSL_VERIFYPEER, false);
        $result = curl_exec($curl);
        curl_close($curl);
        var_dump($result);
}

Here in general NULL
On 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

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mike Grigorieff, 2013-06-25
@Grigorieff

api-yaru.yandex.ru/person/{uid}/post/?oauth_token={token}&format=json&title=Title&content=Content
Are you sending a request right here? If yes, then these parameters will come in the form of GET: $_GET['oauth_token']; and so on, and you need to send these parameters by post to api-yaru.yandex.ru/person/{uid}/post/?format=json, i.e. 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']; 
}

D
Danila Buyanov, 2013-10-23
@DanyBoo

Hello, I started to have such a code, I tried to work in different ways, there were problems with the Russian language, of all the ways only this one worked
.

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;
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question