Sunucu tarafı entegrasyonu

Yayıncılar öncelikli olarak okuyucuları ve okuyucu haklarını yönetmek için sunucu tarafı entegrasyonu kullanır. Esas olarak, yayıncılar Google'ın PPID ile ilgili Ürün Kimliği yararlanma hakkı kaydını güncellemek için UpdateReaderEntitlements kullanır.

Google Cloud kurulumu

Google Cloud'da abonelik bağlamayı yapılandırma işlemi iki ana bileşenden oluşur:

  1. Belirli bir proje için API'yi etkinleştirme
  2. API'ye erişmek için hizmet hesabı oluşturma

Subscription linking API'yi etkinleştirme

Hizmet hesabı kullanmak ve okuyucu yararlanma haklarını yönetmek için Google Cloud projesinde hem Subscription Linking API'nin etkin olması hem de düzgün şekilde yapılandırılmış bir OAuth hizmet hesabı olmalıdır. Bir projede Subscription Linking API'yi etkinleştirmek için menüden -> API'ler ve Hizmetler -> Library'ye gidin ve Subscription Linking araması yapın veya sayfayı doğrudan ziyaret edin:


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

api

Şekil 1. API Kitaplığı'na gitme ve API'yi bir Google Cloud projesi için etkinleştirme.

Hizmet Hesabı oluşturma

Hizmet hesapları, uygulamanızdan Abonelik Bağlama API'sine erişim izni vermek için kullanılır.

  1. Projenizin konsolunda bir hizmet hesabı oluşturun.
  2. Hizmet hesabı için kimlik bilgileri oluşturun ve credentials.json dosyasını uygulamanızın erişebildiği güvenli bir konumda depolayın.
  3. Oluşturduğunuz hizmet hesabına "Abonelik Bağlama Yöneticisi" IAM rolünü verin. Hizmet hesabının özellikleri üzerinde ayrıntılı kontrol için aşağıdaki tablodan uygun rolü atayabilirsiniz.
Özellik / Rol Abonelik Bağlama Yöneticisi Abonelik Bağlama Görüntüleyicisi Abonelik Bağlama Hakları Görüntüleyicisi
Okuyucu hakları alma
Okuyucu kazanın
Okuyucu yararlanma haklarını güncelleme
Okuyucuları silin

Subscription Linking API'yle hizmet hesaplarını kullanma

googleapis istemci kitaplığı aracılığıyla veya istekleri REST API ile imzalayarak Subscription Linking API'ye yapılan çağrıların kimliğini doğrulamak için hizmet hesaplarını kullanın. İstemci kitaplıkları, uygun access_token isteme işlemini otomatik olarak gerçekleştirir. REST API ise bir id_token alıp ardından access_token ile değiştirmeyi gerektirir.

Aşağıdaki istemci kitaplığı ve REST API örnekleri, getReader() uç noktasını kullanır. Tüm API yöntemlerinin canlı gösterimi için Abonelik Bağlama Demosu sitesini veya kodunu inceleyin.

Node.js googleapis istemci kitaplığı ile örnek istek

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 isteklerini manuel olarak imzalama

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
}