溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何將fodi后端部署到cloudbase

發(fā)布時間:2021-11-26 13:57:08 來源:億速云 閱讀:122 作者:小新 欄目:云計(jì)算

這篇文章主要介紹了如何將fodi后端部署到cloudbase,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

上傳和修改后端邏輯:

用cloudbase-cli提交backend-py:

{
  "envId": "default-4gpm7vnrb911600e",
  "functionRoot": "functions",
  "functions": 
  [{
    "name": "fodi",    //fodi就是backend-py
    "timeout": 6,
    "runtime": "Python3.6",
    "memorySize": 128,
    "installDependency": true,
    "handler": "index.main_handler"
  }]
}

以下保證要使用https://xxxx.service.tcloudbase.com/fodi形式的調(diào)用路徑(后臺的接入路徑定義):

def router(event):
    """對多個 api 路徑分發(fā)
    """
    door = 'https://' + event['headers']['host']
    print('door:'+door)
    func_path = '' #event['requestContext']['path'] //置空
    print('func_path:'+func_path)
    
    api = event['path'].replace(func_path, '').strip('/') 
    api_url = door + '/fodi' + event['path']  //加一個fodi串

    queryString = event['queryStringParameters']  //這里改
    body = None
    ......

弄好后,訪問上面的接入路徑,僅/fodi,輸出path error和后面一長串東西就代表服務(wù)器搭建正常,

獲取refresh token

然后就是那個refreshtoken的獲取,http://scfonedrive.github.io已經(jīng)掛掉了,我們可以自建,先在某網(wǎng)站下建一個get.html:

<html><meta charset=utf-8><body><h2>Error</h2><p>Please set the <code>refresh_token</code> in environments<br>
    <a href="" id="a1">Get a refresh_token</a>
    <br><code>allow javascript</code>
    <script>
        url=window.location.href;
        if (url.substr(-1)!="/") url+="/";
        url="https://login.partner.microsoftonline.cn/common/oauth3/v2.0/authorize?scope=https%3A%2F%2Fmicrosoftgraph.chinacloudapi.cn%2FFiles.ReadWrite.All+offline_access&response_type=code&client_id=04c3ca0b-8d07-4773-85ad-98b037d25631&redirect_uri=https://scfonedrive.github.io&state="+encodeURIComponent(url);
        document.getElementById('a1').href=url;
        //window.open(url,"_blank");
    </script>
    </p></body></html>

如果是國際版url換成:
 url="https://login.microsoftonline.com/common/oauth3/v2.0/authorize?scope=https%3A%2F%2Fgraph.microsoft.com%2FFiles.ReadWrite.All+offline_access&response_type=code&client_id=4da3e7f2-bf6d-467c-aaf0-578078f0bf7c&redirect_uri=https://scfonedrive.github.io&state="+encodeURIComponent(url);

再在根下建一個index.html

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
	<head>
		<title>OneManager jump page</title>
	</head>
	<body>
		<a id="direct">No link here!</a><br>
		If not auto jump, click the link to jump.<br>
		如果長時間未跳轉(zhuǎn),請點(diǎn)擊上方鏈接繼續(xù)安裝。<br>
		<label id='test1'></label>
		<script>
      			var q = new Array();
			var query = window.location.search.substring(1);
			//document.getElementById('test1').innerHTML=query;
			var vars = query.split("&");
			for (var i=0;i<vars.length;i++) {
				var pair = vars[i].split("=");
				q[pair[0]]=pair[1];
			}
			var url = q['state'];
			var code = q['code'];
			if (!!url && !!code) {
				url = decodeURIComponent(url);
				if (url.substr(-1)!="/") url+="/";
				var lasturl = url+"?authorization_code&code="+code;
				document.getElementById('direct').innerText = lasturl;
				document.getElementById('direct').href = lasturl;
				window.location = lasturl;
			} else {
				var str='Error! 有誤!';
				if (!url) str+='No url! url參數(shù)為空!';
				if (!code) str+='No code from MS! 微軟code為空!';
				document.getElementById('test1').innerHTML=str+'<br>'+decodeURIComponent(query);
				alert(str);
			}
		</script>
	</body>
</html>

把get中scfonedrive.github.io換成你的index.html所在的網(wǎng)站地址,(之后保證把py后端中用于認(rèn)證的地址和那個clientid,clientsecret替換用你自己新建的一個,具體方法見我前面的一些文章)

調(diào)用后結(jié)果顯示在url中(整個頁面顯示404是沒有處理結(jié)果的php后端,除非你把https://github.com/qkqpttgf/OneDrive_SCF部署在index.html所在的網(wǎng)站),分辨復(fù)制即可。

安排好后端和refreshtoken后,調(diào)用接入路徑/fodi/fodi/,輸出看到其輸出的加密的json結(jié)果,就代表refreshtoken也正常了。 開始部署前端,可以另外一個網(wǎng)站,能托管html的就行。也可以在后端另起一函數(shù),部署如下index.py:

#!/usr/bin/env python
# -*- coding:utf-8 -*-


def main_handler(event, context):
    f = open("./front.html", encoding='utf-8')
    html = f.read()
    return {
        "isBase64Encoded": False,
        "statusCode": 200,
        "headers": {'Content-Type': 'text/html; charset=utf-8'},
        "body": html
    }

front.html當(dāng)然是配置好的那個前端文件。

如果你fodi前端調(diào)用發(fā)生for each,length之類的提示錯誤,往往是refresh token沒獲取對。如果發(fā)生跨域錯誤(chrome f12可看到),則在后端面板中需要配置一條客戶端網(wǎng)站的安全域名。

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“如何將fodi后端部署到cloudbase”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI