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

时间:2022-03-20 04:48:44

The problem

这个问题

I want to send a print job from a python script to a printer that is registered in 'Google Cloud Print'.

我想从python脚本发送一个打印作业到在“谷歌云打印”中注册的打印机。

What I think I need for this

我认为我需要这个!

  • google account

    谷歌账户

  • google cloud api registration: client-id, client-secret (to gain my access token)

    谷歌云api注册:client-id, client-secret(获取我的access token)

  • access token

    访问令牌

    I got my access token by using this tutorial:

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

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

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

What I've tried so far

我已经试过了

Since Bradley Ayers wrote exactly for this purpose a python-library called cloudprinting (https://github.com/bradleyayers/cloudprinting) the following script is meant to deal with my print job:

由于Bradley Ayers为此目的编写了一个名为cloudprinting的python库(https://github.com/bradleyers/cloudprinting),下面的脚本是用来处理我的打印工作的:

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)

Although the script finishes without error messages it doesn't work - which means there is no print job at the end.

虽然该脚本在没有错误消息的情况下完成,但它不能工作——这意味着在末尾没有打印作业。

So I tried to come up with my own attempt of a http post to Google Cloud Print:

因此,我尝试自己设计一个针对谷歌云打印的http post:

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)

Clearly the above requests.post() parameters aren't correct. Which parameters do I need stated in what way? Or more specifically:

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

  1. Is ''https://www.google.com/cloudprint/submit' the correct endpoint?
  2. “https://www.google.com/cloudprint/submit”是正确的端点吗?
  3. What is the correct way of authentication?
  4. 什么是正确的认证方式?
  5. What argument-value-pairs does the variable 'data' need ?
  6. 可变的“数据”需要哪些参数值对?

Any ideas are appreciated. Thanks in advance!

任何想法都是感激。提前谢谢!

1 个解决方案

#1


3  

if anyone still interested using the tutorial to get the access token, i could also get the refresh token with print(credentials.refresh_token), with that I wrote a class that prints, and refresh the token when needed. i could successfully print from python in my cloud printer, also you have to change the scope to "https://www.googleapis.com/auth/cloudprint"

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

hope it helps

希望它能帮助

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)

#1


3  

if anyone still interested using the tutorial to get the access token, i could also get the refresh token with print(credentials.refresh_token), with that I wrote a class that prints, and refresh the token when needed. i could successfully print from python in my cloud printer, also you have to change the scope to "https://www.googleapis.com/auth/cloudprint"

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

hope it helps

希望它能帮助

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)