将时间线卡推送到给定的用户ID

2024-10-02 04:34:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我在玩googlemirrorapi,我正在努力找到一种方法,只把一张卡推给一个特定的用户。你知道吗

当我尝试用以下Python代码发布卡片时,给出了userid(12345677):

 timeline_item = {'text': 'Test10'}
 timeline_item['recipients'] = [{'id':'12345677'}]
 self.mirror_service.timeline().insert(body=timeline_item).execute()

该卡在所有其他测试用户的时间线上可见。你知道吗

我做错什么了?你知道吗


Tags: 方法代码text用户selfidmirrorservice
2条回答

recipients字段没有指定时间线项应该发送给谁。它包含有关谁已被发送的信息卡上的信息-它是为所有回复卡使用,以便能够处理应发送给多人的回复(不一定使用玻璃)。你知道吗

不清楚您使用的是哪种语言,但听起来好像您正在将项目发送给服务的所有经过身份验证的用户。你知道吗

通常,您将指定特定用户的OAuth令牌以写入其时间线。你知道吗

看起来您正在用Python编写代码并使用Google APIs Client Library for Python。要将时间线项目推送到特定的用户id,请在创建镜像服务时设置用户id。Mirror API Python Quickstart在它的notifications code中有一个例子说明了如何做到这一点。recipients字段与将项推送到谁的时间线无关。你知道吗

from oauth2client.appengine import StorageByKeyName
from model import Credentials

self.mirror_service = create_service(
        'mirror', 'v1',
        StorageByKeyName(Credentials, MY_USER_ID, 'credentials').get())
timeline_item = {'text': 'Test10'}
self.mirror_service.timeline().insert(body=timeline_item).execute()

^{}的代码

import httplib2
from apiclient.discovery import build

from model import Credentials

def create_service(service, version, creds=None):
  """Create a Google API service.

  Load an API service from a discovery document and authorize it with the
  provided credentials.

  Args:
    service: Service name (e.g 'mirror', 'oauth2').
    version: Service version (e.g 'v1').
    creds: Credentials used to authorize service.
  Returns:
    Authorized Google API service.
  """
  # Instantiate an Http instance
  http = httplib2.Http()

  if creds:
    # Authorize the Http instance with the passed credentials
    creds.authorize(http)

  return build(service, version, http=http)

相关问题 更多 >

    热门问题