【问题标题】:Is it possible to batch delete contacts using google people api?是否可以使用 google people api 批量删除联系人?
【发布时间】:2021-01-26 05:30:52
【问题描述】:

我发现Contact API中有批量操作。但它是 XML 形式,我更喜欢在 People API 中使用 JSON 形式。有可能吗?

【问题讨论】:

    标签: google-people-api


    【解决方案1】:

    这可以通过使用contactGroup来完成:

    • 使用contactGroup.create 创建一个临时组。
    • 在组中添加所有要删除的成员 contactGroup.members.modify。
    • 使用contactGroup.delete 删除临时组,然后 使用 deleteContacts=true。

    【讨论】:

      【解决方案2】:

      您需要使用 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
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-05
        • 1970-01-01
        • 2021-09-04
        • 2021-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多