python - 将打印作业从python发送到谷歌云打印

标签 python linux google-api python-requests httprequest

问题

我想将打印作业从 python 脚本发送到在“Google 云打印”中注册的打印机。

我认为我需要什么

  • Google 帐户

  • Google 云 API 注册:客户端 ID、客户端密码(用于获取我的访问 token )

  • 访问 token

    我通过使用本教程获得了访问 token :

https://github.com/burnash/gspread/wiki/How-to-get-OAuth-access-token-in-console%3F

到目前为止我已经尝试过

由于 Bradley Ayers 正是为此目的编写了一个名为 cloudprinting ( https://github.com/bradleyayers/cloudprinting ) 的 python 库,因此以下脚本旨在处理我的打印作业:

from cloudprinting import *

access_token = 'xyz_token_xyz' # here my personal access token is stated

auth = OAuth2(access_token, token_type="Bearer")

r = submit_job(printer="e506d12-dbe0-54d3-7392-fd69d45ff2fc", content="some_pdf_in_local_directory.pdf", auth=auth)

虽然脚本完成时没有错误消息,但它不起作用 - 这意味着最后没有打印作业。

因此,我尝试提出自己的尝试:将 http 发布到 Google 云打印:

import requests
import json

access_token = 'xyz_acces_token_xyz' # here my personal access token is stated
printer =  "e506d12-dbe0-54d3-7392-fd69d45ff2fc" # random printer_id 
capabilities = [{}]

data = {"printerid": printer,
    "title": "name",
    "contentType": "pdf",
    "capabilities": json.dumps({"capabilities": capabilities})}

post_request = requests.post('https://www.google.com/cloudprint/submit', headers={'Authorization': access_token},data=data,files='some_pdf_in_local_directory.pdf')

print (post_request.text)

显然上面的 requests.post() 参数不正确。我需要以什么方式说明哪些参数? 或者更具体地说:

  1. 是 '' https://www.google.com/cloudprint/submit ' 正确的端点?
  2. 正确的身份验证方式是什么?
  3. 变量“data”需要什么参数值对?

任何想法都值得赞赏。提前致谢!

最佳答案

如果有人仍然有兴趣使用本教程获取访问 token ,我还可以使用 print(credentials.refresh_token) 获取刷新 token ,我编写了一个可以打印的类,并在需要时刷新 token 。 我可以在我的云打印机中成功地从 python 进行打印,您还必须将范围更改为“https://www.googleapis.com/auth/cloudprint

希望对你有帮助

class CloudPrint():
    def __init__(self,client_id=None, client_secret=None, access_token = None, refresh_token = None):
            self.client_id = client_id
            self.client_secret= client_secret
            self.access_token = access_token
            self.refresh_token = refresh_token
            self.deadline = time.time()-1
        self.api_url ='https://www.google.com/cloudprint/submit'

    def safe_check(self):
        if time.time()>float(self.deadline):
            self.refresh()

    def refresh(self):
        print('google refreshing')
        token = requests.post(
                'https://accounts.google.com/o/oauth2/token',
                data={
                    'client_id': self.client_id,
                    'client_secret': self.client_secret,
                    'grant_type': 'refresh_token',
                    'refresh_token': self.refresh_token,
                }
            )
        self.access_token = token.json()['access_token']
        self.deadline = self.deadline + token.json()['expires_in']
        print('new deadline ', self.deadline)

    def print(self, file, title, printerids):
            self.safe_check()
            body = {"printerid":printerids,
            "title":[title],
            "ticket":"{\r\n  \"version\": \"1.0\",\r\n  \"print\": {}\r\n}",
            }
            files = {'content': open(file,'rb')}

            headers = {"Authorization":"Bearer "+str(self.access_token)}
            print(headers)
            r = requests.post(self.api_url, data=body, files=files, headers=headers)
            print(r.url)
            print(r.text)

if __name__ == '__main__':
    gcp = CloudPrint(CLIENT_ID, CLIENT_SECRET, refresh_token=REFRESH_TOKEN)
    gcp.print('prueba.txt', 'prueba', PRINTER_ID_DEB)

关于python - 将打印作业从python发送到谷歌云打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44439586/

相关文章:

python - Scrapy 返回同一条信息 80 多次

python - 将一串数学函数转换为函数

c++ - 测量缓存行利用率

authorization - 来自服务器的 OAuth2 "Invalid Grant"响应

javascript - 使用 API 和 JavaScript 写入工作表 - CORS block

python - 分析长期运行的 Python 服务器

rdkit.Chem.rdmolfiles.MolToMolBlock(NoneType) 中的 Python 参数类型

linux - 自动更改文件权限

linux - Bash - 查找带空格的文件并使用 sed 重命名

google-api - 是否可以使用 Google OAuth2 使 "redirect_url"不在浏览器窗口中打开?