E
E
Elena2015-01-19 20:38:14
PHP
Elena, 2015-01-19 20:38:14

Get an avatar from ok.ru and mai.ru?

On Facebook and VKontakte, everything is simple - you drive in the user's id and get the user's data.
With classmates and mail, everything is much more complicated. There goes the authorization first. And here the problem arises - how to get an avatar if there is a redirect to a social network and asks to log in. How can I get an avatar without it?

define ('OK_APP_ID', '******');
 define ('OK_APP_PUBLIC', '*******');
define ('OK_APP_SECRET', '******');
define ('OK_URL_CALLBACK', 'http://www.7idei-remonta.ru/otziv.html?action=odnoklasniki');
define ('OK_URL_AUTHORIZE', 'http://www.odnoklassniki.ru/oauth/authorize');
define ('OK_URL_GET_TOKEN', 'http://api.odnoklassniki.ru/oauth/token.do');
define ('OK_URL_ACCESS_TOKEN', 'http://api.odnoklassniki.ru/fb.do');

if (empty($_GET['code'])) {
    self::redirect(OK_URL_AUTHORIZE . '?client_id=' . OK_APP_ID . '&response_type=code' . '&redirect_uri=' . urlencode(OK_URL_CALLBACK));
}

$_SESSION['odnoklasniki']['code'] = $_GET['code'];

if (empty($_SESSION['odnoklasniki']['token'])) {
    $data = array(
          'code' => $_SESSION['odnoklasniki']['code'],
          'redirect_uri' => OK_URL_CALLBACK,
          'client_id' => OK_APP_ID,
          'client_secret' => OK_APP_SECRET,
          'grant_type' => 'authorization_code',
);

    $opts = array('http' =>
        array(
              'method' => 'POST',
               'header' => "Content-type: application/x-www-form-urlencoded\r\n" . "Accept: */*\r\n",
                'content' => http_build_query($data)
         )
      );

     if (!($response = @file_get_contents(OK_URL_GET_TOKEN, false, stream_context_create($opts))))
            self::redirect();

      $result = json_decode($response);

      if (empty($result->access_token)) self::redirect();
      else  $_SESSION['odnoklasniki']['token'] = $result->access_token;
 }

// проверка токена и взятие информации о текущем пользователе
$url = OK_URL_ACCESS_TOKEN .
'?access_token=' . $_SESSION['odnoklasniki']['token'] .
 '&method=users.getInfo' .
 '&application_key=' . OK_APP_PUBLIC .
 '&fields=pic_1' .
 '&uids=' . $val['slogin_id'] .
  '&sig=' . md5('application_key=' . OK_APP_PUBLIC . 'fields=pic_1method=users.getInfouids=' . $val['slogin_id'] .
                                    md5($_SESSION['odnoklasniki']['token'] . OK_APP_SECRET));

 if (!($response = @file_get_contents($url))) self::redirect();

    $user = json_decode($response);

PS In the browser where I entered the social network, everything works well, the problems start if I am not authorized.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Cherednichenko, 2015-01-21
@likeapimp

Have you tried using OFAPI? api.mail.ru/docs/reference/rest/users-getinfo

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question