【发布时间】:2019-03-08 04:55:33
【问题描述】:
我有一个 Google 表格代码,它有一个 UI 部分,即 html 和 code.gs 问题出在 Google 工作表中,我正在获取一个网页,并且身份验证和其他功能运行良好,但是如果我在 code.gs 中添加 doGet() 并将其转换为 webapp,则这些功能无法正常工作,并且 scriptlet 显示在 html 中页面的功能也坏了。
由于 html 页面内的代码在 google app 脚本表的服务器端运行,但由于引用未指向服务器端 code.gs ,因此无法在客户端运行
我需要让它与客户端代码一起工作,它应该像在谷歌电子表格中一样工作
我正在使用 Google Apps Scripts Oauth 并获得一个 facebook 令牌,这是成功生成令牌并访问 facebook 数据的原创作品Original Work
这些是代码
<div class="tab-content">
<!-- Home -->
<div id="ads" class="tab-pane fade in active">
<div id="page-content-wrapper">
<div class="hamburger is-closed" data-toggle="offcanvas">
<span class="hamb-top"></span>
<span class="hamb-middle"></span>
<span class="hamb-bottom"></span>
</div>
<div class="container">
<? if(!getService().hasAccess()) { ?>
<div class="row">
<div class="col-md-12 col-lg-12">
<p style="font-size:25px;">Features:</p>
</div>
</div>
<br />
<div class="row">
<div class="col-md-12" align='center'>
<a class="loginBtn loginBtn--facebook btn btn-info" href='<?=getService().getAuthorizationUrl()?>' target='_blank'>Authorize Facebook</a>
</div>
</div>
<? } else { ?>
<p style='color:green'>Authorization Success</p>
<div class="form-group">
<label for="accountData">Select Account:</label><span class="glyphicon glyphicon-question-sign help" title="Select Account To Export"></span>
<select id="accountData" style="width: 100%">
<? if (getService().hasAccess()) { ?>
<? var data = adAccounts() ?>
<? if (data != false) { ?>
<? data = data['facebookAccountData'] ?>
<optgroup label="Ad Accounts">
<? for (i=0; i<data.length; i++) { ?>
<option value="<?= data[i]['account_id'] ?>" data-type="facebookAds"><?= data[i]['name'] ?> (<?= data[i]['account_id'] ?>)</option>
<? } ?>
</optgroup>
<? } ?>
<? } ?>
</select>
</div>
登录按钮本身在 code.gs 中调用了一个函数,但它只能在 google 工作表中工作,而不能在网络应用程序或任何 html 浏览器或客户端中工作 那么如何在code.gs中引用这个
href='<?=getService().getAuthorizationUrl()
这是code.gs部分
function fbAuth(){
var UI=HtmlService.createTemplate("<b><a href='<?=getService().getAuthorizationUrl()?>' target='_blank'>Click To Authorize</a></b><br /><? if(getService().hasAccess())"+
"{ ?> <?!= <p><span style='color:green'>Authorized Successfully</span></p> } else {?> <?!= <p><span style='color:red'>Not Authorized</span></p> }").evaluate()
SpreadsheetApp.getUi().showModalDialog(UI, "Facebook Authorization")
}
function getService() {
return OAuth2.createService('Facebook')
// Set the endpoint URLs.
.setAuthorizationBaseUrl('https://www.facebook.com/dialog/oauth')
.setTokenUrl('https://graph.facebook.com/v3.2/oauth/access_token')
// Set the client ID and secret.
.setClientId(CLIENT_ID)
.setClientSecret(CLIENT_SECRET)
// Set the name of the callback function that should be invoked to complete
// the OAuth flow.
.setCallbackFunction('authCallback')
//Set Scope
.setScope('ads_read manage_pages publish_pages pages_show_list')
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getUserProperties());
}
function authCallback(request) {
var isAuthorized = getService().handleCallback(request);
if (isAuthorized) {
successUI(true)
showBar()
return HtmlService.createHtmlOutput('Success! You can close this tab.<script>window.top.close()</script>');
} else {
successUI(false)
showBar()
return HtmlService.createHtmlOutput('Denied. You can close this tab.<script>window.top.close()</script>');
}
}
function reset() {
var service = getService();
service.reset();
showBar()
SpreadsheetApp.getUi().alert("Token Reset Success!!")
}
function successUI(isAuth){
if(isAuth){
var UI=HtmlService.createTemplate("<b><span style='color:green'>Authorization Successful</span></b>").evaluate()
SpreadsheetApp.getUi().showModalDialog(UI, "Authorization Status") } else
{var UI=HtmlService.createTemplate("<b><span style='color:red'>Authorization Fail</span></b>").evaluate()
SpreadsheetApp.getUi().showModalDialog(UI, "Authorization Status")}
}
【问题讨论】:
标签: javascript facebook-graph-api google-apps-script google-sheets