首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >python中的swagger_client尝试使用Strava API

python中的swagger_client尝试使用Strava API
EN

Stack Overflow用户
提问于 2019-04-13 01:58:25
回答 1查看 3.3K关注 0票数 3

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

并安装了swagger_client

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

按照他们的指示。但是,当我运行这个应用程序时,我仍然得到import swagger_client ModuleNotFoundError: No module named 'swagger_client'

我的代码在这里

代码语言:javascript
运行
复制
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)

不确定我现在应该安装什么包才能让它正常工作。

EN

回答 1

Stack Overflow用户

发布于 2019-08-16 18:10:27

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

代码语言:javascript
运行
复制
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的文件夹中

代码语言:javascript
运行
复制
java -jar swagger-codegen-cli.jar generate -i https://developers.strava.com/swagger/swagger.json -l python -o generated

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

代码语言:javascript
运行
复制
cd generated && python setup.py install --user && cd ..

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

代码语言:javascript
运行
复制
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写。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55657275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档