gpt4 book ai didi

javascript - URL 缩短器 - 自动缩短 URL

转载 作者:行者123 更新时间:2023-12-03 11:08:10 26 4
gpt4 key购买 nike

简而言之,用户使用 Google Drive Picker API 从 Google 云端硬盘中选择文档,然后生成 URL。我想缩短该 URL,因为当前生成的 URL 太长。

下面是生成 URL 的代码

  // The Browser API key obtained from the Google Developers Console.
var developerKey = 'ID';

// The Client ID obtained from the Google Developers Console.
var clientId = 'ID';

// Scope to use to access user's photos.
var scope = ['https://www.googleapis.com/auth/photos'];

var pickerApiLoaded = false;
var oauthToken;

// Use the API Loader script to load google.picker and gapi.auth.
function onApiLoad() {
gapi.load('auth', {'callback': onAuthApiLoad});
gapi.load('picker', {'callback': onPickerApiLoad});
}

function onAuthApiLoad() {
window.gapi.auth.authorize(
{
'client_id': clientId,
'scope': scope,
'immediate': false
},
handleAuthResult);
}

function onPickerApiLoad() {
pickerApiLoaded = true;
createPicker();
}

function handleAuthResult(authResult) {
if (authResult && !authResult.error) {
oauthToken = authResult.access_token;
createPicker();
}
}

// Create and render a Picker object for picking user Photos.
function createPicker() {
if (pickerApiLoaded && oauthToken) {
var picker = new google.picker.PickerBuilder().
enableFeature(google.picker.Feature.MULTISELECT_ENABLED).
addView(google.picker.ViewId.PDFS).
setOAuthToken(oauthToken).
setDeveloperKey(developerKey).
setCallback(pickerCallback).
build();
picker.setVisible(true);
}
}

// A simple callback implementation.
function pickerCallback(data) {
var url = 'nothing';
if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
var doc = data[google.picker.Response.DOCUMENTS][0];
url = doc[google.picker.Document.URL];
var message = 'The following(s) were stored in Parse: ' + url;
document.getElementById('result').innerHTML = message;

}






}


</script>
</head>
<body>
<div id="result"></div>
<div id="demo">

<!-- The Google API Loader script. -->
<script type="text/javascript" src="https://apis.google.com/js/api.js?onload=onApiLoad"></script>
</body>
</html>

特别是,下面是生成 URL 的部分:

 // A simple callback implementation.
function pickerCallback(data) {
var url = 'nothing';
if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
var doc = data[google.picker.Response.DOCUMENTS][0];
url = doc[google.picker.Document.URL];
var message = 'The following(s) were stored in Parse: ' + url;
document.getElementById('result').innerHTML = message;

}

我已经研究过https://developers.google.com/url-shortener/,但不确定如何集成它。

最佳答案

首先,您必须在 onApiLoad 函数中包含 URL Shortner API 的范围。范围确定后,一旦您向 Drive API 发出请求,您就应该将文档 URL 发送到 URL Shortener API。

您可以依赖类似于以下的函数(基于 https://developers.google.com/api-client-library/javascript/samples/samples 上提供的示例):

<script>
function appendResults(text) {
var results = document.getElementById('results');
results.appendChild(document.createElement('P'));
results.appendChild(document.createTextNode(text));
}

function makeRequest() {
var request = gapi.client.urlshortener.url.insert({
'longURL': 'DRIVE DOCUMENT URL'
});
request.then(function(response) {
appendResults(response.result.longUrl);
}, function(reason) {
console.log('Error: ' + reason.result.error.message);
});
}

function init() {
gapi.client.setApiKey('YOUR API KEY');
gapi.client.load('urlshortener', 'v1').then(makeRequest);
}
</script>
<script src="https://apis.google.com/js/client.js?onload=init"></script>

关于javascript - URL 缩短器 - 自动缩短 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27736431/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com