Skip to content

Commit

Permalink
Fix "vk api error: Invalid request: method is unavailable without acc…
Browse files Browse the repository at this point in the history
…ess token"

Access token for the application has been added.

"Starting from April 2017, to use open methods and secure methods, a service token must be used from the app settings."
(https://vk.com/dev/service_token)
  • Loading branch information
yutkin committed Jul 10, 2017
1 parent 9d679fa commit 16f2db6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
27 changes: 16 additions & 11 deletions vktop/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@
from datetime import timedelta

from .argparser import parse_args
from .constants import VKAPI_URL, VKAPI_VERSION
from .constants import VKAPI_URL, VKAPI_VERSION, APP_ACCESS_KEY
from .utils import get_page_id, VKApiError, pretty_print
from .post import Post

# Define logging parameters for --verbose option
import logging
logging.basicConfig(level=logging.DEBUG,
format='[\033[92m%(levelname)s %(asctime)s\033[0m]: %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p')
logging.basicConfig(
level=logging.DEBUG,
format='[\033[92m%(levelname)s %(asctime)s\033[0m]: %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p')

# Removing noisy debug messages from lib request
requests_logger = logging.getLogger('requests')
Expand All @@ -47,7 +48,11 @@ class PostDownloader:
def __init__(self, page_id, from_date=None, to_date=None):
self.page_id = page_id
self.api_url = VKAPI_URL + 'wall.get'
self.request_params = {'owner_id': self.page_id, 'v': VKAPI_VERSION}
self.request_params = {
'owner_id': self.page_id,
'v': VKAPI_VERSION,
'access_token': APP_ACCESS_KEY
}
self.from_date = from_date or datetime.date.min
self.to_date = to_date or datetime.date.max

Expand Down Expand Up @@ -75,13 +80,13 @@ def fetch(self, init_offset=0, num_to_fetch=None, verbose_mode=False):
self.request_params['count'] = min(num_to_fetch, 100)

if verbose_mode:
LOGGER.debug('{} trying to download {} posts'.format(current_process().name,
num_to_fetch))
LOGGER.debug('{} trying to download {} posts'.format(
current_process().name, num_to_fetch))

fetched_posts = []
fetched_counter = 0
while fetched_counter != num_to_fetch:
response = requests.get(self.api_url, params=self.request_params).json()
response = requests.get(self.api_url, self.request_params).json()

if 'error' in response:
raise VKApiError(response['error']['error_msg'])
Expand All @@ -90,8 +95,8 @@ def fetch(self, init_offset=0, num_to_fetch=None, verbose_mode=False):
fetched_counter += len(posts)

if verbose_mode:
LOGGER.debug('{} downloaded {}/{} posts'.format(current_process().name,
fetched_counter, num_to_fetch))
LOGGER.debug('{} downloaded {}/{} posts'.format(
current_process().name, fetched_counter, num_to_fetch))

for post in posts:
post = Post(
Expand Down Expand Up @@ -153,7 +158,7 @@ def parallel_fetch(self, verbose_mode=False, max_workers=None):

def _distribute_posts(self, total_posts, workers):
""" Uniformly distributes posts for downloading between workers.
Returns next start position for downloading and number of posts to fetch. """
Returns next start position for downloading and number of posts to fetch."""

per_worker = total_posts // workers + total_posts % workers
for offset in range(0, total_posts, per_worker):
Expand Down
1 change: 1 addition & 0 deletions vktop/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import re

APP_ACCESS_KEY = '4c078c244c078c244c078c24614c5279bc44c074c078c241564a36b509cacf5c9242daa'
VKAPI_URL = 'https://api.vk.com/method/'
VKAPI_VERSION = 5.53

Expand Down

0 comments on commit 16f2db6

Please sign in to comment.