本文介绍了python中的swagger_client尝试使用Strava API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use the Stava API in a Flask project. I have seen the following stackoverflow

并安装了swagger_client

and installed swagger_client

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

按照他们的指示.但是,当我运行该应用程序时,我仍然会得到import swagger_clientModuleNotFoundError: No module named 'swagger_client'

as per their instructions. However when i run the app i still get import swagger_clientModuleNotFoundError: 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)

不确定我应该安装什么软件包才能使它正常工作.

not sure what packages i should be installing to get this working now.

推荐答案

首先安装swagger-codegen并检查其是否有效,该示例适用于linux.使用Mac可以更轻松地在其中使用自制软件.

First install swagger-codegen and check that it's working, this example is for linux. Easier with mac where you can use homebrew.

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的,应该存储在名为generate的项目中的文件夹中

After that go in your project and generate the swagger-client. The code below tells that it's for python and should be stored in a folder within the project called generated

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

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

Go into the generated folder and install the requirements

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

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

Change your import statements to refer to the generated folder.

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编写.

Now you can run the file. Ps when you set the access token: configuration needs to be written with upper case C.

这篇关于python中的swagger_client尝试使用Strava API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 20:15