Using Oauth 2.0 for Server to Server Applications
来源:互联网 发布:泰州淘宝网店装修步骤 编辑:程序博客网 时间:2024/05/24 07:34
google官网关于Using Oauth 2.0 for Server to Server Applications的介绍。
首先构造JWT(a header,a claim set,and a signature),然后通过JWT请求Token,以后就可以通过Token调用google api。Token生命周期为1个小时,过期后需要重新请求Token。
难点:根据header和claim set构造signature。
官网上虽然给出了计算步骤,但没有给出用于SHA256withRSA加密的secret key,这样无法验证自己计算出的signature是否正确!最坑的是网上也很难找到C++实现的SHA256withRSA!!
闲话少说,直接给出我是如何在C++下实现SHA256withRSA的。
这里面用到了国外的一个第三方库,https://www.example-code.com/cpp/jwt_rsa_create.asp,我下载的MInGW版.
首先在.pro文件中加入下面两行代码:
LIBS += -L"C:\jwt" -llibchilkat-9.5.0
INCLUDEPATH += $$quote(C:\jwt/include)
#include <CkPrivateKey.h>#include <CkJwt.h>#include <CkGlobal.h>//注册(免费试用30天……)bool Register(){ CkGlobal glob; bool success = glob.UnlockBundle("Anything for 30-day trial"); if (success != true) std::cout << glob.lastErrorText() << "\r\n"; return success;}void CreateJWT(){ Register(); CkPrivateKey privKey; //Create service account,download .json secert key const char *str = "-----BEGIN PRIVATE KEY-----\nMIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDSecDwk2uqokS5\nt/biJQnsj54RCvj2GR9s0213aZ68saxpSVqLGIxoAeaXEZJZ65MUI70ONRwFEF+i\nbN2cABp0smHvD5FYNmWC0zC22rMVgkVMeAKVfG4PkNzI/hDtv13wLmcAL07EoX9v\n+gP9qDdMFhhphqrajSyzFhKwBa5WcSSbJlclE9Wsr2PqA07KucLwzyB3kDjlpK20\npLouy6bkerqN4LnRgp7ZvQNZx++cHBSwWDC6UjOWZ0yS5HV6s24vMSdVuSRYbh/Y\nnaRtmowq4zzG16ldWS57PYDxOZ+F2BtcMmVvkcwyY8FNbWrVToXTWYV6OZAsutde\ns+C/SBR3AgMBAAECggEAHjOSV/MPdACs0+lt561g6y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PSAVX/h7P5JYHQ6fvC15YfFb0DlJqhfPme/PL2cUB8gVYge/284GhxMISdJF\ndPcTqTQ7Jsb7VNJVwV53TEEpsJGoZIj7hotxF94oyopIIEUfPHDTojr0t0KlbvZp\nB6KP00xKU+BWUzz7NSU6hQ==\n-----END PRIVATE KEY-----"; bool success = privKey.LoadPem(str); if (success != true) { std::cout << privKey.lastErrorText() << "\r\n"; return; } CkJwt jwt; const char *header = "{\"alg\": \"RS256\",\"typ\": \"JWT\"}"; const char *claimset = "{" "\"iss\": \"761326798069-r5mljlln1rd4lrbhg75efgigp36m78j5@developer.gserviceaccount.com\"," "\"scope\": \"https://www.googleapis.com/auth/devstorage.full_control\"," "\"aud\": \"https://www.googleapis.com/oauth2/v4/token\"," "\"exp\": 1479988670," "\"iat\": 1479985670" "}"; jwt.put_AutoCompact(true); // Create the JWT. const char *token = jwt.createJwtPk(header,claimset,privKey); std::cout << token << "\r\n";}
最后计算得到的JWT:
0 0
- Using Oauth 2.0 for Server to Server Applications
- 【实例图文详解】OAuth 2.0 for Web Server Applications
- Applications Server
- Developing Web Applications for WebLogic Server
- Using OAuth 2.0 to Access Google APIs
- SQL Server 2008 R2 Linked Server to DB2 using Microsoft OLEDB provider for odbc. Fail
- Using Async RPC with Your Client/Server Applications
- Debugging WebLogic Server Applications Using Eclipse and the WebLogic-Plugin
- OAuth 2.0 Authorization Server 代码copy
- OAuth 2.0 Support with WSO2 Identity Server
- 搭建oauth-server
- SQL Server Connection Strings for ASP.NET Web Applications
- How to check Oracle Applications Web Server Version ?
- Using JConsole to Monitor Applications
- Using JConsole to Monitor Applications
- using http upload file to web server
- Pass parameters to server using hidden field
- BlackBerry Enterprise Server for MDS Applications” 4.1.7 could do data push without email server
- 社区医疗系统--总结(一)
- 盒模型代码简写
- I2C器件的从设备地址的设置(以AT24C02为例)
- 利用python 爬虫下载那啥种子
- Direct3D设备管理器(Direct3D Device Manager)
- Using Oauth 2.0 for Server to Server Applications
- js获取当前页面名称
- JavaScript求质数算法(一)
- 【研发管理】三手项目 接盘时 必须要做的四件事
- Java中JFrame.getContentPane().add(pane)与JFrame.add(pane)的区别
- 配置PropertiesEditor插件 jp.gr.java_conf.ussiy.app.propedit_5.3.3
- --概率论部分总结--
- 51nod 1020 逆序排列
- Text I/O