सर्वर-साइड इंटिग्रेशन

पब्लिशर, सर्वर-साइड इंटिग्रेशन का इस्तेमाल मुख्य रूप से उनका कॉन्टेंट पढ़ने वाले लोगों और उनके अधिकारों को मैनेज करने के लिए करेंगे. मुख्य तौर पर, पब्लिशर किसी पीपीआईडी के लिए, प्रॉडक्ट आईडी के अधिकार से जुड़े Google के रिकॉर्ड को अपडेट करने के लिए, UpdateReaderEntitlements का इस्तेमाल करेंगे.

Google Cloud का सेटअप

Google Cloud में Subscription Linking API कॉन्फ़िगर करने की प्रक्रिया में दो अहम चरण शामिल हैं:

  1. दिए गए प्रोजेक्ट के लिए, एपीआई चालू करना
  2. एपीआई ऐक्सेस करने के लिए, सेवा खाता बनाना

Subscription Linking API चालू करना

सेवा खाते का इस्तेमाल करने और लोगों के अधिकारों को मैनेज करने के लिए, Google Cloud प्रोजेक्ट में Subscription Linking API चालू होना चाहिए और सही तरीके से कॉन्फ़िगर किया गया OAuth सेवा खाता मौजूद होना चाहिए. किसी प्रोजेक्ट के लिए Subscription Linking API चालू करने के लिए, मेन्यू -> एपीआई और सेवाएं -> लाइब्रेरी पर जाएं और Subscription Linking खोजें या सीधे इस पेज पर जाएं:


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

api

पहला डायग्राम. एपीआई लाइब्रेरी पर जाना और Google Cloud प्रोजेक्ट के लिए एपीआई चालू करना.

सेवा खाता बनाना

सेवा खातों का इस्तेमाल, आपके ऐप्लिकेशन से Subscription Linking API को ऐक्सेस करने की अनुमति देने के लिए किया जाता है.

  1. अपने प्रोजेक्ट के कंसोल में, सेवा खाता बनाएं.
  2. सेवा खाते के लिए क्रेडेंशियल बनाएं और credentials.json फ़ाइल को किसी ऐसी सुरक्षित जगह पर सेव करें जहां से आपका ऐप्लिकेशन इसे ऐक्सेस कर सके.
  3. आपने जो सेवा खाता बनाया है उसके लिए, "सदस्यता लिंक करने वाले एडमिन" को IAM की भूमिका दें. सेवा खाते की क्षमताओं पर बेहतर कंट्रोल के लिए, नीचे दी गई टेबल से सही भूमिका असाइन की जा सकती है.
क्षमता / भूमिका सदस्यता लिंक करने वाला एडमिन सदस्यता लिंक करने वाला व्यूअर सदस्यता लिंक करने की एनटाइटलमेंट व्यूअर
लोगों के एनटाइटलमेंट पाएं
पाठक पाएं
लोगों के एनटाइटलमेंट अपडेट करना
पाठकों को मिटाएं

Subscription Linking API के साथ सेवा खाते इस्तेमाल करना

Subscription Linking API की पुष्टि का अनुरोध करने के लिए, सेवा खातों का इस्तेमाल करें. इसके लिए इन खातों का इस्तेमाल या तो googleapis क्लाइंट लाइब्रेरी के साथ करें या REST API पर अनुरोध करके करें. क्लाइंट लाइब्रेरी, सही access_token का अनुरोध करके पुष्टि को अपने-आप मैनेज कर लेती हैं, जबकि REST API को id_token की ज़रूरत होती है, जिसे access_token से बदला जाता है.

क्लाइंट लाइब्रेरी और REST API के नीचे दिए गए दोनों उदाहरणों में, getReader() एंडपॉइंट का इस्तेमाल किया जाता है. एपीआई के सभी तरीकों को सामने इस्तेमाल होता देखने के लिए,Subscription Linking Demo साइट या इसका कोड देखें.

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
}