gpt4 book ai didi

python - python 中的 swagger_client 尝试使用 Strava API

转载 作者:行者123 更新时间:2023-12-04 15:46:16 32 4
gpt4 key购买 nike

我正在尝试在 Flask 项目中使用 Stava API。我看到了以下 stackoverflow

并安装了 swagger_client

swagger-codegen 生成 -i https://developers.strava.com/swagger/swagger.json -l python -o ./StravaPythonClient

按照他们的指示。但是,当我运行该应用程序时,我仍然得到 import swagger_client
ModuleNotFoundError:没有名为“swagger_client”的模块

我的代码在这里

import swagger_client
from swagger_client.rest import ApiException
from pprint import pprint

# Configure OAuth2 access token for authorization: strava_oauth
swagger_client.configuration.access_token = 'fe931c21b503a46b61b1000000000000000000000'

# create an instance of the API class
api_instance = swagger_client.StreamsApi()
id = 2284367626 # Long | The identifier of the activity.
#keys = # array[String] | Desired stream types.
keyByType = true # Boolean | Must be true. (default to true)

try:
# Get Activity Streams
api_response = api_instance.getActivityStreams(id, keys, keyByType)
pprint(api_response)
except ApiException as e:
print("Exception when calling StreamsApi->getActivityStreams: %s\n" % e)

不确定我现在应该安装什么包来让它工作。

最佳答案

首先安装 swagger-codegen 并检查它是否正常工作,这个例子是针对 linux 的。在 Mac 上使用自制软件更容易。

wget https://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.13/swagger-codegen-cli-2.4.13.jar -O swagger-codegen-cli.jar
java -jar swagger-codegen-cli.jar help

之后进入您的项目并生成 swagger-client。下面的代码表明它是用于 python 的,应该存储在名为 generated 的项目中的文件夹中

java -jar swagger-codegen-cli.jar generate -i https://developers.strava.com/swagger/swagger.json -l python -o generated

进入生成的文件夹并安装要求

cd generated && python setup.py install --user && cd ..

更改导入语句以引用生成的文件夹。

from generated import swagger_client
from generated.swagger_client.rest import ApiException
from pprint import pprint

# Configure OAuth2 access token for authorization: strava_oauth
swagger_client.Configuration.access_token = 'fe931c21b503a46b61b1000000000000000000000'

# create an instance of the API class
api_instance = swagger_client.StreamsApi()
id = 2284367626 # Long | The identifier of the activity.
#keys = # array[String] | Desired stream types.
keyByType = true # Boolean | Must be true. (default to true)

try:
# Get Activity Streams
api_response = api_instance.getActivityStreams(id, keys, keyByType)
pprint(api_response)
except ApiException as e:
print("Exception when calling StreamsApi->getActivityStreams: %s\n" % e)

现在您可以运行文件了。 ps 设置access token时:configuration需要写大写C。

关于python - python 中的 swagger_client 尝试使用 Strava API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55657275/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com