H
H
henre19892018-12-20 13:53:53
Python
henre1989, 2018-12-20 13:53:53

How to place large posts in Odnoklassniki via API?

There is a working script for posting, it works with posts that are small in terms of text.

mydict='{ "media": [ { "type": "text", "text": "'+text_post+'" }, { "type": "photo", "list": ['+pic_LIST+']}], "publishAtMs": "'+date+'", "onBehalfOfGroup": "true"}'
p1='application_key='+key+'attachment='+mydict+'format=jsongid='+id_gr+'method=mediatopic.posttype='+self.type_post+session
h=hashlib.md5(p1.encode("UTF-8"))
sig=h.hexdigest()
att = urllib.parse.quote(mydict, safe='utf-8')

#Загрузка поста
url='https://api.ok.ru/fb.do?application_key='+key+'&attachment='+str(att)+'&format=json&gid='+id_gr+'&method=mediatopic.post&type='+self.type_post+'&sig='+sig+'&access_token='+access_token
       
r = requests.post(url)
print(r.text)

after translating
att = urllib.parse.quote(mydict, safe='utf-8') we
get a huge link that is sent to the server and I get the answer "414 Request-URI Too Large"
I don't know how to win this, I read that you can use post request, but the result is the same
link to the method in ok https://apiok.ru/dev/methods/rest/mediatopic/media...
I make a post request with parameters in the request
mydict='{ "media": [ { "type": "text", "text": "'+text+'" }, { "type": "photo", "list": [{ "id":"'+pic_tokken+'"}]}], "publishAtMs": "'+date+'", "onBehalfOfGroup": "true"}'

att = urllib.parse.quote(mydict, safe='utf-8')
        
p1={'application_key': app_key,
                    'attachment':mydict,
                    'format':'json',
                    'gid':id_gr,
                    'method':'mediatopic.post',
                    'type':post_type2
                    }
h=hashlib.md5(str(p1).encode('utf-8')+'{token}{secret}'.format(token=access_token,secret=privat_key).encode('utf-8'))
sig=h.hexdigest()
print(sig)
        
r = requests.post('https://api.ok.ru/fb.do', params={'application_key':app_key, 'attachment':str(att), 'format':'json', 'gid':id_gr, 'method':'mediatopic.post', 'type':post_type2}, data={'access_token': access_token, 'sig':sig})

now the problem is that the md5 check does not converge
Made by analogy with the php code
$params = array(
    "application_key"   =>  $ok_public_key,
    "method"            =>  "mediatopic.post",
    "gid"               =>  $ok_group_id,
    "type"              =>  "GROUP_THEME",
    "attachment"        =>  $attachment,
    "format"            =>  "json",
);

// Подпишем
$sig = md5( arInStr($params) . md5("{$ok_access_token}{$ok_private_key}") );

$params['access_token'] = $ok_access_token;
$params['sig']          = $sig;

$step3 = json_decode( getUrl("https://api.ok.ru/fb.do", "POST", $params, 30, false, false ), true);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
albertalexandrov, 2018-12-20
@henre1989

You have generated a url for a get request.
You need to form the request like this:
where the query parameters will be in the data dictionary.
Or change requests.post(url) to requests.get(url)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question