我在 https://github.com/GoogleChrome/chrome-app-samples/tree/master/samples/one-time-payment 上发现了一个很棒的资源。
我对其进行了一些编辑,加入了更多的错误处理,然后将其移植到了我的应用程序中。然而,我很多次不得不去修改不同的 Google API 并进行编辑。如果您在使用此代码时遇到问题,我会尽力查找我所做的修改,并让您知道。您可以将此复制并粘贴到后台页面的控制台窗口中,并使用 getLicense 执行它。如果您将您的密钥复制到清单文件中,则可以使用本地扩展程序执行操作,而无需每次更改都需要等待一个小时。我强烈建议这样做。密钥是来自开发者仪表板的那个-有关更多信息请参见。
function getLicense() {
var CWS_LICENSE_API_URL = 'https://www.googleapis.com/chromewebstore/v1.1/userlicenses/';
xhrWithAuth('GET', CWS_LICENSE_API_URL + chrome.runtime.id, true, onLicenseFetched);
}
function onLicenseFetched(error, status, response) {
function extensionIconSettings(badgeColorObject, badgeText, extensionTitle ){
chrome.browserAction.setBadgeBackgroundColor(badgeColorObject);
chrome.browserAction.setBadgeText({text:badgeText});
chrome.browserAction.setTitle({ title: extensionTitle });
}
var licenseStatus = "";
if (status === 200 && response) {
response = JSON.parse(response);
licenseStatus = parseLicense(response);
} else {
console.log("FAILED to get license. Free trial granted.");
licenseStatus = "unknown";
}
if(licenseStatus){
if(licenseStatus === "Full"){
window.localStorage.setItem('ChromeGuardislicensed', 'true');
extensionIconSettings({color:[0, 0, 0, 0]}, "", "appname is enabled.");
}else if(licenseStatus === "None"){
extensionIconSettings({color:[255, 0, 0, 230]}, "?", "appnameis disabled.");
}else if(licenseStatus === "Free"){
window.localStorage.setItem('appnameislicensed', 'true');
extensionIconSettings({color:[255, 0, 0, 0]}, "", window.localStorage.getItem('daysLeftInappnameTrial') + " days left in free trial.");
}else if(licenseStatus === "unknown"){
window.localStorage.setItem('appnameislicensed', 'true');
extensionIconSettings({color:[200, 200, 0, 100]}, "?", "appnameis enabled, but was unable to check license status.");
}
}
window.localStorage.setItem('appnameLicenseCheckComplete', 'true');
}
function parseLicense(license) {
var TRIAL_PERIOD_DAYS = 1;
var licenseStatusText;
var licenceStatus;
if (license.result && license.accessLevel == "FULL") {
console.log("Fully paid & properly licensed.");
LicenseStatus = "Full";
} else if (license.result && license.accessLevel == "FREE_TRIAL") {
var daysAgoLicenseIssued = Date.now() - parseInt(license.createdTime, 10);
daysAgoLicenseIssued = daysAgoLicenseIssued / 1000 / 60 / 60 / 24;
if (daysAgoLicenseIssued <= TRIAL_PERIOD_DAYS) {
window.localStorage.setItem('daysLeftInCGTrial', TRIAL_PERIOD_DAYS - daysAgoLicenseIssued);
console.log("Free trial, still within trial period");
LicenseStatus = "Free";
} else {
console.log("Free trial, trial period expired.");
LicenseStatus = "None";
}
} else {
console.log("No license ever issued.");
LicenseStatus = "None";
}
return LicenseStatus;
}
function xhrWithAuth(method, url, interactive, callback) {
console.log(url);
var retry = true;
var access_token;
getToken();
function getToken() {
console.log("Calling chrome.identity.getAuthToken", interactive);
chrome.identity.getAuthToken({ interactive: interactive }, function(token) {
if (chrome.runtime.lastError) {
callback(chrome.runtime.lastError);
return;
}
console.log("chrome.identity.getAuthToken returned a token", token);
access_token = token;
requestStart();
});
}
function requestStart() {
console.log("Starting authenticated XHR...");
var xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.setRequestHeader('Authorization', 'Bearer ' + access_token);
xhr.onreadystatechange = function (oEvent) {
if (xhr.readyState === 4) {
if (xhr.status === 401 && retry) {
retry = false;
chrome.identity.removeCachedAuthToken({ 'token': access_token },
getToken);
} else if(xhr.status === 200){
console.log("Authenticated XHR completed.");
callback(null, xhr.status, xhr.response);
}
}else{
console.log("Error - " + xhr.statusText);
}
}
try {
xhr.send();
} catch(e) {
console.log("Error in xhr - " + e);
}
}
}