การผสานรวมฝั่งเซิร์ฟเวอร์

ผู้เผยแพร่เนื้อหาจะใช้การผสานรวมฝั่งเซิร์ฟเวอร์เพื่อจัดการผู้อ่านและการให้สิทธิ์เป็นหลัก โดยส่วนใหญ่ ผู้เผยแพร่โฆษณาจะใช้ UpdateReaderEntitlements เพื่ออัปเดตบันทึกของ Google เกี่ยวกับการให้สิทธิ์รหัสผลิตภัณฑ์สำหรับ PPID

การตั้งค่า Google Cloud

การกำหนดค่าการลิงก์การสมัครใช้บริการใน Google Cloud มีคอมโพเนนต์หลัก 2 องค์ประกอบดังนี้

  1. การเปิดใช้ API สำหรับโปรเจ็กต์ที่กำหนด
  2. การสร้างบัญชีบริการสำหรับการเข้าถึง API

เปิดใช้ Subscription Linking API

หากต้องการใช้บัญชีบริการและจัดการการให้สิทธิ์ของผู้อ่าน โปรเจ็กต์ Google Cloud ต้องเปิดใช้ทั้ง Subscription Linking API และบัญชีบริการ OAuth ที่กำหนดค่าอย่างถูกต้อง หากต้องการเปิดใช้ Subscription Linking API สำหรับโปรเจ็กต์ ให้ไปที่เมนู -> API และบริการ -> ไลบรารี และค้นหา Subscription Linking หรือไปที่หน้านี้โดยตรง


https://console.cloud.google.com/apis/library?project=gcp_project_id

api

รูปที่ 1 การไปยังไลบรารี API และเปิดใช้ API สำหรับโปรเจ็กต์ Google Cloud

สร้างบัญชีบริการ

บัญชีบริการมีไว้เพื่อให้สิทธิ์แอปเข้าถึง Subscription Linking API จากแอปพลิเคชัน

  1. สร้างบัญชีบริการในคอนโซลของโปรเจ็กต์
  2. สร้างข้อมูลเข้าสู่ระบบสำหรับบัญชีบริการ และจัดเก็บไฟล์ credentials.json ไว้ในตำแหน่งที่ปลอดภัยที่แอปพลิเคชันของคุณเข้าถึงได้
  3. มอบบทบาท IAM "ผู้ดูแลระบบการลิงก์การสมัครใช้บริการ" แก่บัญชีบริการที่คุณสร้างขึ้น หากต้องการควบคุมความสามารถของบัญชีบริการอย่างละเอียด คุณสามารถมอบหมายบทบาทที่เหมาะสมได้จากตารางต่อไปนี้
ความสามารถ / บทบาท ผู้ดูแลระบบการลิงก์การสมัครใช้บริการ ผู้ดูการลิงก์การสมัครใช้บริการ ผู้ดูการให้สิทธิ์การลิงก์การสมัครใช้บริการ
รับการให้สิทธิ์ผู้อ่าน
รับผู้อ่าน
อัปเดตการให้สิทธิ์ผู้อ่าน
ลบผู้อ่าน

ใช้บัญชีบริการกับ Subscription Linking API

ใช้บัญชีบริการเพื่อตรวจสอบสิทธิ์การเรียกใช้ Subscription Linking API ไม่ว่าจะด้วยไลบรารีของไคลเอ็นต์ googleapis หรือโดยการลงนามคำขอด้วย REST API ไลบรารีของไคลเอ็นต์จะจัดการขอ access_token ที่เหมาะสมโดยอัตโนมัติ ในขณะที่ API ของ REST จำเป็นต้องดึงข้อมูล id_token แล้วแลกเปลี่ยนกับ access_token

ทั้งไลบรารีไคลเอ็นต์และตัวอย่าง REST API ต่อไปนี้จะใช้ปลายทาง getReader() ดูการสาธิตวิธี API ทั้งหมดแบบเรียลไทม์ได้ที่เว็บไซต์การสาธิตการลิงก์การสมัครใช้บริการหรือโค้ดของเว็บไซต์

ตัวอย่างคำขอที่มีไลบรารีของไคลเอ็นต์ Node.js googleapis

import {readerrevenuesubscriptionlinking_v1, Auth} from 'googleapis'
const subscriptionLinking = readerrevenuesubscriptionlinking_v1.Readerrevenuesubscriptionlinking

class SubscriptionLinking {
  constructor() {
    this.auth = new Auth.GoogleAuth({
      keyFile: process.env.KEY_FILE,
      scopes: [
        'https://www.googleapis.com/auth/readerrevenue.subscriptionlinking.manage'
      ],
    })
  }

  init() {
    return new subscriptionLinking(
        {version: 'v1', auth: this.auth})
  }
}

const api = new SubscriptionLinking()
const client = api.init()

async function getReader (ppid) {
  const publicationId = process.env.PUBLICATION_ID
  return await client.publications.readers.get({
    name: `publications/${publicationId}/readers/${ppid}`,
  })
}

การลงนามในคำขอ REST API ด้วยตนเอง

import fetch from 'node-fetch'
import jwt from 'jsonwebtoken'

function getSignedJwt() {
  /*
    Either store the credentials string in an environmental variable
    Or implement logic to fetch it.
  */
  const key_file = process.env.CREDENTIALS_STRING

  const issueDate = new Date()
  const expireMinutes = 60
  const offsetInSeconds = issueDate.getTimezoneOffset() * 60000
  const expireDate = new Date(issueDate.getTime() + (expireMinutes * 60000))
  const iat = Math.floor((issueDate.getTime() + offsetInSeconds) / 1000)
  const exp = Math.floor((expireDate.getTime() + offsetInSeconds) / 1000)

  const token = {
    iss: key_file.client_email,
    iat,
    exp,
    aud: 'https://oauth2.googleapis.com/token',
    scope:'https://www.googleapis.com/auth/readerrevenue.subscriptionlinking.manage',
  }
  return jwt.sign(token, key_file.private_key, {
    algorithm: 'RS256',
    keyid: key_file.private_key_id,
  })
}

async function getAccessToken(signedJwt) {
  let body = new URLSearchParams();
  body.set('grant_type', 'urn:ietf:params:oauth:grant-type:jwt-bearer')
  body.set('assertion', signedJwt)
  const request = await fetch('https://oauth2.googleapis.com/token', {
    method: 'POST',
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    body
  })

  const accessResponse = await accessFetch.json()
  return accessResponse.access_token
}

async function getReader(ppid) {
  const publicationId = process.env.PUBLICATION_ID
  const base_url = 'https://readerrevenuesubscriptionlinking.googleapis.com/v1'
  const endpoint = `${base_url}/publications/${publicationId}/readers/${ppid}`
  const signedJwt = await getSignedJwt()
  const accessToken = await getAccessToken(signedJwt)

  const reader = await fetch(endpoint, {
     method: 'GET',
     headers: {
       Authorization: `Bearer ${accessToken}`,
     },
   }).then((response) => {
    return response.json()
  })

  return reader
}