批量更新联系人 - 谷歌人api

时间:2018-01-21 08:22:50

标签: google-people

有没有办法与新的谷歌人api批量CRUD联系人(我看到READS的getBatchGet存在)?如果我们从旧的gdata联系人api升级,我的应用程序将左右限速率。

2 个答案:

答案 0 :(得分:1)

按照 Google People APIs 了解如何填充对象,最重要的部分是在 do-while 循环中使用 Google Batch API 的方式:


const { google }  = require('googleapis')


function extractJSON (str) {
  const result = []

  let firstOpen  = 0
  let firstClose = 0
  let candidate  = 0

  firstOpen = str.indexOf('{', firstOpen + 1)

  do {
    firstClose = str.lastIndexOf('}')

    if ( firstClose <= firstOpen ) {
      return []
    }

    do {
      candidate = str.substring(firstOpen, firstClose + 1)

      try {
        result.push(JSON.parse(candidate))
        firstOpen = firstClose
      } catch (e) {
        firstClose = str.substr(0, firstClose).lastIndexOf('}')
      }

    } while ( firstClose > firstOpen )

    firstOpen = str.indexOf('{', firstOpen + 1)

  } while ( firstOpen !== -1 )

  return result
}

async function batchDeleteContacts (resourceIds) {
  /*
    resourceIds = [
      'c1504716451892127784',
      'c1504716451892127785',
      'c1504716451892127786,
      ....'
    ]


    Setup your google-api client and extract the oauth header/
  */
  const authHeader = await google.oAuth2Client.getRequestHeaders()

  let counter   = 0
  let confirmed = []

  try {
    do {
      const temp = resourceIds.splice(0, 25)

      const multipart = temp.map((resourceId, index) => ({
        'Content-Type': 'application/http',
        'Content-ID': (counter * 25) + index,
        'body': `DELETE /v1/people/${resourceId}:deleteContact HTTP/1.1\n`
      }))

      const responseString = await request.post({
        url: 'https://people.googleapis.com/batch',
        method: 'POST',
        multipart: multipart,
        headers: {
          'Authorization': authHeader.Authorization,
          'content-type': 'multipart/mixed'
        }
      })

      const result = extractJSON(responseString)
      confirmed = confirmed.concat(result)

      counter ++

    } while ( resourceIds.length > 0 )

  } catch (ex) {

    // Handling exception here
  }

  return confirmed
}

async function batchInsertContacts (contacts) {
  /*
    Follow the Google People APIs documentation to learn how to generate contact objects,
    Its easy and depends on your needs.

    contacts = [{
      resource: {
        clientData,
        names,
        nicknames,
        birthdays,
        urls,
        addresses,
        emailAddresses,
        phoneNumbers,
        biographies,
        organizations
      }
    }]

    Setup your google-api client and extract the oauth header/
  */
  const authHeader = await google.oAuth2Client.getRequestHeaders()

  let counter   = 0
  let confirmed = []

  const authHeader = await this.oAuth2Client.getRequestHeaders()

  try {
    do {
      const temp = contacts.splice(0, 25)

      const multipart = temp.map((contact, index) => ({
        'Content-Type': 'application/http',
        'Content-ID': (counter * 25) + index,
        'body': 'POST /v1/people:createContact HTTP/1.1\n'
              + 'Content-Type: application/json\n\n'
              + JSON.stringify(contact.resource)
      }))

      const responseString = await request.post({
        url: 'https://people.googleapis.com/batch',
        method: 'POST',
        multipart: multipart,
        headers: {
          'Authorization': authHeader.Authorization,
          'content-type': 'multipart/mixed'
        }
      })

      const result = extractJSON(responseString)
      confirmed = confirmed.concat(result)

      counter ++

    } while ( contacts.length > 0 )

  } catch (ex) {

    // Handling exception here
  }

  return confirmed
}

async function batchUpdateContacts (contacts) {
  /*
    Follow the Google People APIs documentation to learn how to generate contact objects,
    Its easy and depends on your needs.

    contacts = [{
      resourceId: 'c1504616451882127785'
      resource: {
        clientData,
        names,
        nicknames,
        birthdays,
        urls,
        addresses,
        emailAddresses,
        phoneNumbers,
        biographies,
        organizations
      }
    }]

    Setup your google-api client and extract the oauth header/
  */
  const authHeader = await google.oAuth2Client.getRequestHeaders()

  const updatePersonFields = 'names,nicknames,birthdays,urls,addresses,emailAddresses,phoneNumbers,biographies,organizations'

  let counter   = 0
  let confirmed = []

  const authHeader = await this.oAuth2Client.getRequestHeaders()

  try {
    do {
      const temp = contacts.splice(0, 25)

      const multipart = temp.map((contact, index) => ({
        'Content-Type': 'application/http',
        'Content-ID': (counter * 25) + index,
        'body': `PATCH /v1/people/${contact.resourceId}:updateContact?updatePersonFields=${updatePersonFields} HTTP/1.1\n`
              + 'Content-Type: application/json\n\n'
              + JSON.stringify(contact.resource)
      }))

      const responseString = await request.post({
        url: 'https://people.googleapis.com/batch',
        method: 'POST',
        multipart: multipart,
        headers: {
          'Authorization': authHeader.Authorization,
          'content-type': 'multipart/mixed'
        }
      })

      const result = extractJSON(responseString)
      confirmed = confirmed.concat(result)

      counter ++

    } while ( contacts.length > 0 )

  } catch (ex) {

    // Handling exception here
  }

  return confirmed
}

答案 1 :(得分:0)

简答:不。

但是,您可以使用quotaUser查询参数来阻止速率限制。

  
      
  • 允许您甚至从服务器端应用程序强制执行每用户配额   在用户的IP地址未知的情况下。这可能发生,因为   例如,使用在App Engine上运行cron作业的应用程序   用户代表。
  •   
  • 您可以选择唯一的任意字符串   标识用户,但限制为40个字符。
  •   

我一直在使用People API进行新的开发。似乎非常有限,例如联系人搜索甚至还没有。我会保留Contacts API / GData,以获取尚未在新API中提供的功能。