Javascript Chrome网上商店免费试用付款扩展
我正试图将我在Chrome网上商店发布的Google Chrome扩展转换成一个免费试用版,使用他们新的授权API——但是Google的文档让我非常困惑。见: 此外,OpenID2.0似乎已被弃用 是否有某种形式的插入代码来设置免费试用,并根据授权API检查用户?我有很多用户,我不想把他们搞得一团糟,迫使他们撞上支付墙——他们应该免费得到资助。我在网上找不到其他人这样做是为了查看他们的代码并理解它们 理想情况下,我的扩展应该在7天内完全可用,然后免费试用期满,并要求用户付款Javascript Chrome网上商店免费试用付款扩展,javascript,google-chrome-extension,oauth-2.0,Javascript,Google Chrome Extension,Oauth 2.0,我正试图将我在Chrome网上商店发布的Google Chrome扩展转换成一个免费试用版,使用他们新的授权API——但是Google的文档让我非常困惑。见: 此外,OpenID2.0似乎已被弃用 是否有某种形式的插入代码来设置免费试用,并根据授权API检查用户?我有很多用户,我不想把他们搞得一团糟,迫使他们撞上支付墙——他们应该免费得到资助。我在网上找不到其他人这样做是为了查看他们的代码并理解它们 理想情况下,我的扩展应该在7天内完全可用,然后免费试用期满,并要求用户付款 我感谢这里的任何帮助
我感谢这里的任何帮助 我在网站上找到了一个很好的资源 当我将它移动到我的应用程序中时,我对它进行了一些编辑以添加更多的错误捕获。我有很多次不得不去不同的GoogleAPI上编辑它们。如果您有这个代码的问题,我会尝试查找我更改了什么,并让您知道。您可以将其复制并粘贴到后台页面的控制台窗口中,并使用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"){
//chrome.browserAction.setIcon({path: icon}); to disabled - grayed out?
extensionIconSettings({color:[255, 0, 0, 230]}, "?", "appnameis disabled.");
//redirect to a page about paying as well?
}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"){
//this does mean that if they don't approve the permissions,
//it works free forever. This might not be ideal
//however, if the licensing server isn't working, I would prefer it to work.
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');
}
/*****************************************************************************
* Parse the license and determine if the user should get a free trial
* - if license.accessLevel == "FULL", they've paid for the app
* - if license.accessLevel == "FREE_TRIAL" they haven't paid
* - If they've used the app for less than TRIAL_PERIOD_DAYS days, free trial
* - Otherwise, the free trial has expired
*****************************************************************************/
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";
//open a page telling them it is not working since they didn't pay?
}
} else {
console.log("No license ever issued.");
LicenseStatus = "None";
//open a page telling them it is not working since they didn't pay?
}
return LicenseStatus;
}
/*****************************************************************************
* Helper method for making authenticated requests
*****************************************************************************/
// Helper Util for making authenticated XHRs
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);
}
}
}
函数getLicense(){
var CWS_许可证_API_URL='1〕https://www.googleapis.com/chromewebstore/v1.1/userlicenses/';
xhrWithAuth('GET',CWS\u LICENSE\u API\u URL+chrome.runtime.id,true,onlicenseetched);
}
函数onLicenseFetched(错误、状态、响应){
函数ExtensionSettings(badgeColorObject、badgeText、extensionTitle){
chrome.browserAction.setBadgeBackgroundColor(badgeColorObject);
chrome.browserAction.setBadgeText({text:badgeText});
chrome.browserAction.setTitle({title:extensionTitle});
}
var licenseStatus=“”;
如果(状态===200&&response){
response=JSON.parse(response);
licenseStatus=parseLicense(响应);
}否则{
log(“未能获得许可证。授予免费试用。”);
licenseStatus=“未知”;
}
if(许可证状态){
如果(licenseStatus==“完整”){
setItem('ChromeGuardislicensed','true');
ExtensionSettings({color:[0,0,0,0]},“,”appname已启用。“);
}else if(licenseStatus==“无”){
//chrome.browserAction.setIcon({path:icon});是否禁用-灰显?
ExtensionSettings({color:[255,0,0,230]},“?”,“appnameis disabled.”);
//重定向到关于支付的页面?
}else if(licenseStatus==“免费”){
setItem('appnameislicensed','true');
ExtensionSettings({color:[255,0,0,0]},”,window.localStorage.getItem('daysLeftInappnameTrial')+“免费试用剩余天数”);
}else if(licenseStatus==“未知”){
//这意味着如果他们不批准权限,
//它永远是免费的。这可能不太理想
//但是,如果授权服务器不工作,我希望它能工作。
setItem('appnameislicensed','true');
ExtensionSettings({color:[200,200,0,100]},“?”,“AppName已启用,但无法检查许可证状态。”);
}
}
setItem('appnameLicenseCheckComplete','true');
}
/*****************************************************************************
*解析许可证并确定用户是否应获得免费试用
*-如果license.accessLevel==“FULL”,则他们已为该应用付费
*-如果license.accessLevel==“免费试用”,则他们尚未付款
*-如果他们使用该应用的试用期少于天,则免费试用
*-否则,免费试用已过期
*****************************************************************************/
函数解析许可证(许可证){
var试验期=1天;
var-licenseStatusText;
风险值许可状态;
如果(license.result&&license.accessLevel==“完整”){
console.log(“完全付费并获得适当许可”);
LicenseStatus=“完整”;
}else if(license.result&&license.accessLevel==“免费试用”){
var daysagolicenseisued=Date.now()-parseInt(license.createdTime,10);
DaysAgolicenseisued=DaysAgolicenseisued/1000/60/60/24;
如果(Daysagolicensensues起诉Karl,太棒了-今晚我将再次深入讨论,并编辑此评论,让您知道我发现了什么。关于您的错误-查找您的客户id应该非常简单-请参阅此帖子:Karl-似乎您在未指定名称的情况下遇到了此问题-请参阅此处的解决方案,我一直得到一个“无法”的答案调用chrome.identity.getAuthToken时,请阅读未定义的属性“getAuthToken”-有什么想法吗?Karl,我只是好奇,你是如何在已经安装了你的应用程序的用户中添加grandfathering的?我正在试图找出使用许可证执行此操作的最佳方法。createdTimeI收到一个错误[1]当我尝试使用上述代码获取许可证时。我遵循了回答中提到的相同步骤。这是否意味着谷歌许可证API在服务器端存在一些问题?[1]: