将 python 2 代码更新为 python 3 googleads api 代码

Updating python 2 code to python 3 code for googleads api

我正在尝试将 google 广告 api 与 python 3 一起使用,但我遇到了他们的 generate_refresh_token.py 文件的问题。该文件已针对 python 3 进行了更新,但我需要对其进行调试,因为它仍然包含一些 python 2 代码。例如,打印语句中没有 () 并且有一个使用 raw_input() 而不是 input() 的实例。

无论如何,我收到一条我无法理解的错误消息。有人可以帮我吗?

我试过用谷歌搜索解决方案,但我有点迷路了。

代码从第110行开始到第122行结束:

print ('Access token: %s') % flow.credentials.token
print ('Refresh token: %s') % flow.credentials.refresh_token


if __name__ == '__main__':
  args = parser.parse_args()
  configured_scopes = [SCOPE]
  if not (any([args.client_id, DEFAULT_CLIENT_ID]) and
          any([args.client_secret, DEFAULT_CLIENT_SECRET])):
    raise AttributeError('No client_id or client_secret specified.')
  if args.additional_scopes:
    configured_scopes.extend(args.additional_scopes.replace(' ', '').split(','))
  main(args.client_id, args.client_secret, configured_scopes)

该代码应该抛出一个我可以使用的访问令牌,但它给了我这个错误:

Access token: %s
Traceback (most recent call last):
  File "generate_refresh_token.py", line 122, in <module>
    main(args.client_id, args.client_secret, configured_scopes)
  File "generate_refresh_token.py", line 110, in main
    print ('Access token: %s') % flow.credentials.token
TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'

我相信这也是一个 python 2 vs python 3 的问题,如果有人能帮助我解决这个问题,我将不胜感激!

您的括号有错字。打印应该是:

print('Access token: %s'% flow.credentials.token) 
print('Refresh token: %s' % flow.credentials.refresh_token)

为了安全使用格式:

print('Access token: {}'.format(flow.credentials.token))
print('Refresh token: {}'.format(flow.credentials.refresh_token))

更新打印语句。

print ('Access token: {}'.format(flow.credentials.token))
print ('Refresh token: {}'.format(flow.credentials.refresh_token))