Yayıncılar, okuyucuları ve haklarıyla ilgili işlemleri yönetmek için öncelikle sunucu tarafı entegrasyonu kullanır. Yayıncılar, öncelikle Google'ın bir PPID için ürün kimliği hakkıyla ilgili kaydını güncellemek amacıyla UpdateReaderEntitlements
kullanır.
Google Cloud kurulumu
Google Cloud'da abonelik bağlamayı yapılandırmanın iki ana bileşeni vardır:
- Belirli bir proje için API'yi etkinleştirme
- API'ye erişmek için hizmet hesabı oluşturma
Subscription Linking API'yi etkinleştirme
Hizmet hesabı kullanmak ve okuyucunun haklarından yararlanmak için Google Cloud projesinde hem Abonelik Bağlantısı API'sinin etkinleştirilmesi hem de düzgün yapılandırılmış bir OAuth hizmet hesabının bulunması gerekir. Bir proje için Subscription Linking API'yi etkinleştirmek üzere menü -> API'ler ve Hizmetler -> Kitaplık'a gidin ve Subscription Linking
'yi arayın veya sayfayı doğrudan ziyaret edin:
https://console.cloud.google.com/apis/library?project=gcp_project_id
Şekil 1. API kitaplığına gitme ve bir Google Cloud projesi için API'yi etkinleştirme.
Hizmet hesabı oluşturma
Hizmet hesapları, uygulamanızdan Subscription Linking API'ye erişime izin vermek için kullanılır.
- Projenizin konsolunda bir hizmet hesabı oluşturun.
- Hizmet hesabı için kimlik bilgileri oluşturun ve
credentials.json
dosyasını uygulamanızın erişebileceği güvenli bir konumda saklayın. - 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 sahibi olmak için aşağıdaki tablodan uygun rolü atayabilirsiniz.
Yetenek / rol | Abonelik Bağlama Yöneticisi | Abonelik Bağlama Görüntüleyicisi | Abonelik Bağlama Yararlanma Hakları Görüntüleyicisi |
---|---|---|---|
Okuyucu ayrıcalıklarını alma | |||
Okuyucu edinme | |||
Okuyucuların yararlanma haklarını güncelleme | |||
Okuyucuları silme |
Abonelik Bağlantı API'si ile hizmet hesaplarını kullanma
Abonelik Bağlama API'sine yapılan çağrıların kimliğini doğrulamak için hizmet hesaplarını kullanın. Bu işlemi googleapis istemci kitaplığı ile veya istekleri REST API ile imzalayarak yapabilirsiniz. İstemci kitaplıkları, uygun access_token
'yi isteme işlemini otomatik olarak gerçekleştirirken REST API'de bir id_token
'nin alınması ve ardından access_token
ile değiştirilmesi gerekir.
Aşağıdaki istemci kitaplığı ve REST API örneklerinde getReader()
uç noktası kullanılır. Tüm API yöntemlerinin canlı bir gösterimi için Subscription Linking Demo sitesine veya koduna bakın.
node.js googleapis istemci kitaplığıyla ö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}`,
});
};
async function updateEntitlements(ppid) {
const publicationId = process.env.PUBLICATION_ID;
const requestBody = {
/*
Refer to
https://developers.google.com/news/subscribe/subscription-linking/appendix/glossary#entitlements_object
*/
entitlements : [{
product_id: `${publicationId}:basic`,
subscription_token: 'abc1234',
detail: 'This is our basic plan',
expire_time: '2025-10-21T03:05:08.200564Z'
}]
};
return await client.publications.readers.updateEntitlements({
name: `publications/${publicationId}/readers/${ppid}/entitlements`,
requestBody
});
};
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
}