The Things Network LoRaWAN Stack V3 学习笔记 2.2 使用 CLI 进行 OAuth 登录

版权声明:本文为twowinter原创文章,请联系微信公众号twowinter,未经博主允许不得转载。 https://blog.csdn.net/iotisan/article/details/88808710

前言

在上一篇笔记中已经将 Stack 运行起来了,现在可以着手用 CLI 来做一些测试,第一步是要先登录账户。

小能手这段时间在学习 The Things Network LoRaWAN Stack V3,从使用和代码等角度对该 Stack 进行了分析,详细可点此查看

1 获取 OAUTH 链接

ubuntu@VM-0-6-ubuntu:~/lorawan-stack$ ./ttn-lw-cli login
  INFO Please go to https://localhost:8885/oauth/authorize?client_id=cli&redirect_uri=http%3A%2F%2Flocalhost%3A11885%2Foauth%2Fcallback&response_type=code
  INFO Waiting for your authorization...

由于这个 Stack 我是跑在 CVM 上,又懒得装桌面系统,所以在本地浏览器访问CVM,将 localhost 做调整。

https://CVM_IP:8885/oauth/authorize?client_id=cli&redirect_uri=http%3A%2F%2Flocalhost%3A11885%2Foauth%2Fcallback&response_type=code

我之前编译有问题,看日志返回了无效的css/js 文件,因此浏览器页面无法展示:

  INFO Request handled                          duration=22.686898ms location=/o
auth/login?n=%2Foauth%2Fauthorize%3Fclient_id%3Dcli%26redirect_uri%3Dhttp%253A%2
52F%252Flocalhost%253A11885%252Foauth%252Fcallback%26response_type%3Dcode method
=GET namespace=web remote_addr=14.17.22.45:37767 request_id=01D6G1YDJD59JVYPB8EN
8WG31Z response_size=0 status=302 url=/oauth/authorize?client_id=cli&redirect_ur
i=http%3A%2F%2Flocalhost%3A11885%2Foauth%2Fcallback&response_type=code
  INFO Request handled                          duration=30.772272ms method=GET
namespace=web remote_addr=14.17.22.45:37767 request_id=01D6G1YDN20MVZJ1C7T37NJ2C
N response_size=1286 status=200 url=/oauth/login?n=%2Foauth%2Fauthorize%3Fclient
_id%3Dcli%26redirect_uri%3Dhttp%253A%252F%252Flocalhost%253A11885%252Foauth%252F
callback%26response_type%3Dcode
  INFO Request handled                          duration=4.221753ms method=GET n
amespace=web remote_addr=14.17.22.45:37767 request_id=01D6G1YDRAE2RER4Z3P2R22KZF
 response_size=0 status=200 url=/assets/oauth.css
  INFO Request handled                          duration=2.191717ms method=GET n
amespace=web remote_addr=14.17.22.45:37767 request_id=01D6G1YDRCW6TTSBQSVG12HMNG
 response_size=0 status=200 url=/assets/oauth.js

正常顺利的话,我们可以看到登录页面。

2 帐号注册

没有账号的话,需要先注册账号。这边TTN没处理好,WEB没有给与应该有的提示。

我注册时发现无法点击,看了调试日志才发现,原来无法注册“ttn”、“ttn_1”。

  INFO Finished unary call                      duration=268.898µs error=error:p
kg/identityserver/blacklist:blacklisted_id (the ID `ttn` can not be registered)
error_correlation_id=7d461d3db0f84c8f9a1004a3de4309fe error_name=blacklisted_id
error_namespace=pkg/identityserver/blacklist grpc_code=InvalidArgument grpc_meth
od=Create grpc_service=ttn.lorawan.v3.UserRegistry namespace=grpc request_id=01D
6F5D5NB0RC7J2AKT6HTQKY7
  INFO Finished unary call                      duration=76.578µs error=error:pk
g/ttnpb:identifiers (invalid identifiers) error_cause=invalid field UserID: valu
e 'ttn_1' must be a string conforming to regex "^[a-z0-9](?:[-]?[a-z0-9]){2,}$"
error_correlation_id=959871736a304a57b55737e52a89684d error_name=identifiers err
or_namespace=pkg/ttnpb grpc_code=InvalidArgument grpc_method=Create grpc_service
=ttn.lorawan.v3.UserRegistry namespace=grpc request_id=01D6F5DH4XJ5GVM87YQXRDV25
D

最终是使用 ttn-1 注册成功了。

grpc_service=ttn.lorawan.v3.UserRegistry namespace=db query=SELECT "id" FROM "co
ntact_info_validations"  WHERE (id = $1) request_id=01D6FEFM6EDV1N565BKNERG50Y r
ows=1 source=contact_info_store.go:171 values=[60c3ce12-7482-4661-abf2-e7c49c39a
c06]
  INFO Created email validation token           email=[email protected] grpc_method=Cre
ate grpc_service=ttn.lorawan.v3.UserRegistry namespace=grpc request_id=01D6FEFM6
EDV1N565BKNERG50Y token=XIPV2CTVSG54D57MZDZ6HBFUVYWWQ7PQZSJQF26JP4KU7A5UBLDQ
  WARN Could not send email without email provider body=Please confirm your emai
l address for The Things Network Stack for LoRaWAN.

Your email address will be used as contact for user "ttn-1".

Reference: UDHB4FEG2UIBEQUFPG5BO5WMUEHOCLMEREOCLKQ
Confirmation Token: XIPV2CTVSG54D57MZDZ6HBFUVYWWQ7PQZSJQF26JP4KU7A5UBLDQ
 grpc_method=Create grpc_service=ttn.lorawan.v3.UserRegistry namespace=grpc requ
est_id=01D6FEFM6EDV1N565BKNERG50Y subject=Please confirm your email address for
The Things Network Stack for LoRaWAN to=[email protected]
  INFO Finished unary call                      duration=207.139986ms grpc_metho
d=Create grpc_service=ttn.lorawan.v3.UserRegistry namespace=grpc request_id=01D6
FEFM6EDV1N565BKNERG50Y

3 账号登录,网页重定向

本能手在这边经历过一个坑,我把两处 localhost 都做了替换,即 https://CVM_IP:8885/oauth/authorize?client_id=cli&redirect_uri=http%3A%2F%2FCVM_IP%3A11885%2Foauth%2Fcallback&response_type=code

但浏览器立即提示:
HTTP error: urls don’t validate: http://localhost:11885/oauth/callback;/oauth/code / http://CVM_IP:11885/oauth/callback

只能先改一个,https://CVM_IP:8885/oauth/authorize?client_id=cli&redirect_uri=http%3A%2F%2Flocalhost%3A11885%2Foauth%2Fcallback&response_type=code

在出现重定向网页提示无法连接时,再把重定向部分的localhost替换成 CVM_IP。

$ ./ttn-lw-cli login
  INFO Please go to https://localhost:8885/oauth/authorize?client_id=cli&redirect_uri=http%3A%2F%2Flocalhost%3A11885%2Foauth%2Fcallback&response_type=code
  INFO Waiting for your authorization...
  INFO Got OAuth access token

END


猜你喜欢

转载自blog.csdn.net/iotisan/article/details/88808710
今日推荐