如何将fodi后端部署到cloudbase

这篇文章主要介绍了如何将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形式的调用路径(后台的接入路径定义):

def router(event):
    """对多个 api 路径分发
    """
    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和后面一长串东西就代表服务器搭建正常,

获取refresh token

然后就是那个refreshtoken的获取,http://scfonedrive.github.io已经挂掉了,我们可以自建,先在某网站下建一个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>
		如果长时间未跳转,请点击上方链接继续安装。<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参数为空!';
				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所在的网站地址,(之后保证把py后端中用于认证的地址和那个clientid,clientsecret替换用你自己新建的一个,具体方法见我前面的一些文章)

调用后结果显示在url中(整个页面显示404是没有处理结果的php后端,除非你把https://github.com/qkqpttgf/OneDrive_SCF部署在index.html所在的网站),分辨复制即可。

安排好后端和refreshtoken后,调用接入路径/fodi/fodi/,输出看到其输出的加密的json结果,就代表refreshtoken也正常了。 开始部署前端,可以另外一个网站,能托管html的就行。也可以在后端另起一函数,部署如下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当然是配置好的那个前端文件。

如果你fodi前端调用发生for each,length之类的提示错误,往往是refresh token没获取对。如果发生跨域错误(chrome f12可看到),则在后端面板中需要配置一条客户端网站的安全域名。

感谢你能够认真阅读完这篇文章,希望小编分享的“如何将fodi后端部署到cloudbase”这篇文章对大家有帮助,同时也希望大家多多支持蜗牛博客,关注蜗牛博客行业资讯频道,更多相关知识等着你来学习!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

评论

有免费节点资源,我们会通知你!加入纸飞机订阅群

×
天气预报查看日历分享网页手机扫码留言评论电报频道链接