将API集成到现有的Qt/QML项目中

huangapple go评论136阅读模式
英文:

integrate api to an existing Qt/qml project

问题

这是翻译好的部分:

"这是我第一次使用Qt/qml和API,我正在尝试根据国家在我的项目中添加祷告时间...我观看了许多关于添加REST API的视频和Qt文档,但仍然没有很好的思路如何做这个。

要理解我想做的事情:

*我有一个我想要添加的API
*我在YouTube上搜索了如何在项目中添加REST API,但这与在Qt/qml项目中应该做的方式完全不同

有人能帮助吗?
(我确实在许多Qt文档和YouTube上搜索过,但仍然需要学习更多)"

英文:

it's my first time using Qt/qml and API , i am trying to add like prayer time to my project depend on country ... i watched many videos and QT doc about adding rest api but i still cound not get the good idea how to do this .

To inderstand what i want to do :

*i have API that i want to add
*i searched on youtube how to add a rest api on a project but it's totally different i should do in a Qt/qml project

Is there anyone could help?
(i really have searched on many Qt doc and on youtube and i still need to learn more )

答案1

得分: 1

访问Qt或任何平台中的REST API将归结为三个步骤:

  1. 设置API凭据
  2. 获取授权以进行调用
  3. 调用API

对于第一步,设置API凭据。您需要访问您打算使用的REST API的网站,然后按照它们的步骤创建客户端密钥、应用程序ID、回调URL等。然后,您需要保存这些信息以在第二步中使用。通常,您可以下载一个JSON或Plist文件。

对于第二步:获取授权,您需要使用QOAuthorizationCodeFlow。您可以参考我的回答,其中包含Google API的示例。示例Qt OAuth Flow

对于第三步:调用REST API。您可以使用上面Google API示例中的最后一步。或者,您可以像下面这样组合一个网络请求(您需要使用Qt网络授权模块)。下面的示例中的URL需要与您使用的API所需的URL匹配,包括密钥和令牌等。您需要创建一个具有.cpp和.h文件的类。

在.cpp文件中:

nam = new QNetworkAccessManager(this);
auto rep = nam->get(QUrl("https://www.googleapis.com/oauth2/v1/userinfo?alt=json"));
QEventLoop loop;
connect(rep, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
QString currentByteArray = rep->readAll();
qDebug() << "network reply google api connect step get info" << currentByteArray;

然后在.h文件中:

private:
QNetworkAccessManager *nam = nullptr;

最后,在.pro文件中添加:

QT += network networkauth

希望这有所帮助。

当我学习API时,我使用Postman帮助我在Qt中进行操作之前生成API URL。一旦我熟悉了如何构建URL并确保它正常工作,我就尝试在Qt中使其全部运行。

英文:

To access rest API in qt or in any platform will boil down to three steps:

  1. Setting up API credentials
  2. Obtaining Authorization to make calls
  3. Making calls to the API

For step 1. Setting up your api credentials. You will need to go to the website of the REST API you intent to use, then follow their steps to create a client key and application id, callback url etc. Then you will need to save that information to be used in step 2. Usually you can download a json or plist

For Step 2: Obtaining Authorization, you will need to use the QOAuthorizationCodeFlow. You can see my answer to this question for an example with google api. example qt oauthflow

For Step 3: Making a call to the REST API. You could use the last step in the google api sample above. Or you can just put a network request together like this (you will need the qt network authorization module for this). The URL in the example below would need to match the required url for the API you are using including keys and tokens etc. You will need to make a class with .cpp and .h files.

in the .cpp file

nam = new QNetworkAccessManager(this);
auto rep = nam-&gt;get(QUrl(&quot;https://www.googleapis.com/oauth2/v1/userinfo?alt=json&quot;));
            QEventLoop loop;
            connect(rep, &amp;QNetworkReply::finished, &amp;loop, &amp;QEventLoop::quit);
            loop.exec();
            QString currentByteArray = rep-&gt;readAll();
            qDebug() &lt;&lt; &quot;network reply google api connect step get info&quot; &lt;&lt; currentByteArray;

then in the .h file

private:
QNetworkAccessManager *nam = nullptr;

then the .pro file

QT += network networkauth

I hope this helps.

When I was learning about API's, I used postman to help me generate the API Urls prior to having to doing it in Qt. Once I was comfortable with how to build the Url and was sure it worked, I then tried to make it all work within Qt.

huangapple
  • 本文由 发表于 2023年5月24日 21:58:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76324340.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定