尝试使用stravaapi的python中的swagger_客户端

2024-09-30 04:35:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在烧瓶项目中使用stavaapi。我看到了下面的stackoverflow

并安装了swagger®客户端

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

按照他们的指示。但是当我运行这个应用程序时,我仍然得到import swagger_client ModuleNotFoundError: No module named '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)

不知道我应该安装什么软件包来让这个工作。在


Tags: oftheinstancefromimportclienttokenapi
1条回答
网友
1楼 · 发布于 2024-09-30 04:35:23

首先安装swagger codegen并检查它是否正常工作,这个示例适用于linux。在mac上更容易使用自制软件。在

wget http://central.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.7/swagger-codegen-cli-2.4.7.jar -O swagger-codegen-cli.jar
java -jar swagger-codegen-cli.jar help

然后进入你的项目并生成一个招摇过市的客户机。下面的代码说明它是为python编写的,应该存储在项目中名为generated的文件夹中

^{pr2}$

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

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当您设置访问令牌时:配置需要用大写的C编写

相关问题 更多 >

    热门问题