如何通过vk_api将照片上传到我的社区主相册

huangapple go评论88阅读模式
英文:

How to upload photo into main album at my community via vk_api

问题

If I use login and password to auth I get "vk_api.exceptions.ApiError: [203] Access to group denied"

If I use access token I get "vk_api.exceptions.ApiError: [27] Group authorization failed: method is unavailable with group auth."

First way:

def upload_pic(filename, album_id, group_id):
    vk_session = vk_api.VkApi(login=LOGIN, password=PASSWORD)
    try:
        vk_session.auth(token_only=True)
    except vk_api.AuthError as error_msg:
        print(error_msg)
        return
    upload = vk_api.VkUpload(vk_session)

    upload.photo(filename, album_id, group_id=group_id)

Second way:

def upload_pic(filename, album_id, group_id):
    vk_session = vk_api.VkApi(token=TOKEN)
    upload = vk_api.VkUpload(vk_session)
    upload.photo(filename, album_id, group_id=group_id)

I tried to add auth block from the first way to the second one, but it didn't change anything.

英文:

If I use login and password to auth I get "vk_api.exceptions.ApiError: [203] Access to group denied"

If I use access token I get "vk_api.exceptions.ApiError: [27] Group authorization failed: method is unavailable with group auth."

First way:

def upload_pic(filename, album_id, group_id):
    vk_session = vk_api.VkApi(login=LOGIN, password=PASSWORD)
    try:
        vk_session.auth(token_only=True)
    except vk_api.AuthError as error_msg:
        print(error_msg)
        return
    upload = vk_api.VkUpload(vk_session)

    upload.photo(filename, album_id, group_id=group_id)

Second way:

def upload_pic(filename, album_id, group_id):
    vk_session = vk_api.VkApi(token=TOKEN)
    upload = vk_api.VkUpload(vk_session)
    upload.photo(filename, album_id, group_id=group_id)

I tried to add auth block from first way to second one but it nothing changed
rights for token

答案1

得分: 0

我不确定这是否是原因,但我记得vk_api使用关键字参数。

总的来说,这是可工作的代码:

def upload_pic(filename, album_id, group_id):
    vk_session = vk_api.VkApi(login=LOGIN, password=PASSWORD)
    try:
        vk_session.auth(token_only=True)
    except vk_api.AuthError as error_msg:
        print(error_msg)
        return
    upload = vk_api.VkUpload(vk_session)
    upload.photo(filename, album_id=album_id, group_id=group_id)
英文:

I'm not sure that this is the reason but i remembered that vk_api uses keyword args

In general here's the working code

def upload_pic(filename, album_id, group_id):
    vk_session = vk_api.VkApi(login=LOGIN, password=PASSWORD)
    try:
        vk_session.auth(token_only=True)
    except vk_api.AuthError as error_msg:
        print(error_msg)
        return
    upload = vk_api.VkUpload(vk_session)
    upload.photo(filename, album_id=album_id, group_id=group_id)

huangapple
  • 本文由 发表于 2023年4月19日 22:12:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055552.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定