为了账号安全,请及时绑定邮箱和手机立即绑定

如何以编程方式将数据输入到 python 'input()' 提示中?

如何以编程方式将数据输入到 python 'input()' 提示中?

忽然笑 2021-11-23 19:34:29
我正在为 Spotify API 使用 python 包装器。https://spotipy.readthedocs.io/en/latest/#installation作为授权过程的一部分,Spotify API 让用户在其默认 Web 浏览器中登录 Spotify,然后将它们发送到预定义的(当您向 Spotify 注册应用程序时)REDIRECT_URI。此 REDIRECT_URI 当前设置为 localhost:6969。登录后,它会生成一个 input()(死亡输入),供您将重定向到的 URI 复制粘贴到命令提示符中。我真的不想在使用命令提示符时向随机的人出售。目标是在 localhost:6969 (起诉我)上打开一个烧瓶服务器,然后双击它的 /authorize 页面(发送到 oauth2 登录然后捕获代码)http://flask.pocoo.org/docs/1.0/所以 -我在 jflask.py 中保留我的 Flask 装备和 Spotify 捕手:from flask import Flask, request, redirect, sessionfrom requests_oauth2 import OAuth2from spotipy import Spotifyimport spotipy.util as utilimport requestsimport datetimeapp = Flask(__name__)spotify_auth = OAuth2(  client_id='e33c6fa0d6a249ccafa232a9cf62a616',  client_secret='a43b0b6a4de14b97b4e468459e0f7824',  redirect_uri="http://localhost:6969/authorize",  site="https://accounts.spotify.com",  authorization_url="/authorize",  token_url="/api/token",  scope_sep=" ")# and then use this url as a link within a public login viewprint(spotify_auth.authorize_url(  scope=["user-read-recently-played", "user-top-read"],  response_type="code"  ))# and have an authorize route that can direct you to Spotify or handle you #being redirected back here from Spotify@app .route('/authorize')def authorize():  print('triggered')  error = request.args.get("error")  if error:    abort(404)  code = request.args.get("code")  if code:    data = spotify_auth.get_token(      code=code,      grant_type="authorization_code",    )    access_token = data["access_token"]    refresh_token = data["refresh_token"]    scopes = data["scope"].split()    self.sp = spotipy.Spotify(auth=access_token)    print(access_token)    return self.sp我已经尝试将 Popen 与 stdout=PIPE 一起使用。经过进一步的研究,我很确定这也阻碍了我的成功。该规范旨在与 subprocess.communicate() 一起使用https://docs.python.org/3/library/subprocess.html#subprocess.PIP感谢大家的帮助。
查看完整描述

2 回答

?
慕田峪9158850

TA贡献1794条经验 获得超7个赞

Spotipy 的prompt_for_user_token方法是作为一种快速而肮脏的方法提供的,以允许在本地使用该模块的人启动和运行,并且如果您希望任意用户能够通过网站对自己进行身份验证,则它不打算成为应该直接构建的代码. 由于您有一个 Flask 应用程序,您可能应该拥有它和您的 Spotipy 代码直接通过导入而不是使用管道和标准 i/o 进行交互。

需要研究的一些事情是:

  • Flask-Login如果您想管理登录网站的用户

  • 将身份验证传递给spotipy.Spotify访问令牌(客户端凭据管理器或requests会话)以外的替代方法。这些其他方法允许 Spotipy 客户端在访问令牌在使用中过期时刷新访问令牌,处理用户撤销等。

  • requests-oauth2(请注意,这只是众多 OAuth2 Python 库中的一个,可能不是最好的),它使所有授权内容更加方便,并允许您执行以下操作:

# import Flask things, requests-oauth2, 

from requests_oauth2 import OAuth2

from spotipy import Spotify

from flask import redirect


spotify_auth = OAuth2(

    client_id=os.environ["SPOTIFY_CLIENT_ID"],

    client_secret=os.environ["SPOTIFY_CLIENT_SECRET"],

    redirect_uri=f"http://{os.environ['HOST_NAME']}/authorize",

    site="https://accounts.spotify.com",

    authorization_url="/authorize",

    token_url="/api/token",

    scope_sep=" "

)


# and then use this url as a link within a public login view

spotify_auth.authorize_url(

    scope=["user-read-recently-played", "user-top-read"],

    response_type="code"

)


# and have an authorize route that can direct you to Spotify or handle you being redirected back here from Spotify

@app.route('/authorize')

def authorize():


    error = request.args.get("error")

    if error:

        abort(404)


    code = request.args.get("code")

    if code:

        data = spotify_auth.get_token(

            code=code,

            grant_type="authorization_code",

        )

        access_token = data["access_token"]

        refresh_token = data["refresh_token"]

        scopes = data["scope"].split()

        spotify_client = spotipy.Spotify(auth=access_token)

    else:

        return redirect(spotify_auth.authorize_url(

            scope=["user-read-recently-played", "user-top-read"],

            response_type="code",

            state=session['state']

        ))

在第一次访问 时/authorize,它会将用户重定向到 Spotify 的登录页面。但是当用户在那里成功登录时,它会将用户重定向回 Flask 站点(返回/authorize),而不是执行prompt_for_user_token().

一旦它们重新打开/authorize,这次有一个code请求参数 - 因此它会发出 OAuth2 客户端到提供者请求来将此代码转换为您的访问令牌。

这两者都prompt_for_user_token遵循相同的方法:

  1. 获取 Spotify 授权 URL 以引导用户登录

  2. 处理来自 Spotify 登录的重定向 - Flask 通过接收来自 Spotify 重定向到的 URL 的输入来prompt_for_user_token执行此操作,并通过让您从不存在的网页复制/粘贴 URL 来执行此操作。

  3. 调用以将代码转换为用户的访问和刷新令牌。

我可能对这段代码做了一些修改,因为它是从我的 Spotify 集成的旧 Flask 实现中精心挑选出来的。这是一个更完整的要点,但我很确定很多 SQLAlchemy 的东西,授权视图,非常糟糕,所以请加一点盐。


查看完整回答
反对 回复 2021-11-23
?
侃侃无极

TA贡献2051条经验 获得超10个赞

您可以尝试修改 util 模块。用 response = raw_input() 将 try-except 块替换为:

response = sys.stdin.readline().strip()

如果尚未导入,请不要忘记导入 sys。

这应该允许正常的 PIPE-ing。

或者您可以使用 pexpect 库而不是 subprocess 模块。它知道如何处理调整终端 fcntl 标志或在 Windows 上使用 mscvrt 的输入。

此外,当 PIPE-ing 数据时,不要忘记 raw_input()、input() 或 sys.stdin.readline() 在它们接收到适当的行尾字符之前不会返回。"\r"、"\n" 或 "\r\n"。您之前是否使用授权 URL 发送过?


查看完整回答
反对 回复 2021-11-23
  • 2 回答
  • 0 关注
  • 231 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信