gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 11:41:37 26 4
gpt4 key购买 nike

问题

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

我认为我需要做些什么

  • 谷歌账户

  • google cloud api 注册:client-id,client-secret(获取我的访问 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 post to Google Cloud Print的尝试:

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. 变量“数据”需要哪些参数值对?

任何想法都会受到赞赏。提前致谢!

最佳答案

如果有人仍然对使用本教程获取访问 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com