当前位置: 首页 > 工具软件 > Gwibber > 使用案例 >

Gwibber 中国区登陆twitter,facabook,sina的修改

池麒
2023-12-01

      最近才换成ubuntu 12.04 。默认界面中有个广播功能,就是gwibber。试了一下,发现twitter,facebook,sina好像都有问题。于是就研究了下代码,python写的很好修改

     关于twitter和facebook都是个https的问题,打开源代码把所有http换成https即可

     关于sina就比较蛋疼了,因为Gwibber里的插件是俺Oauth1.0写的,现在sina换成Oauth2.0了,所以所有认证都要变,所幸api密钥还可以用

     没仔细研究oauth那个库的用法,就直接用requests模拟oauth2.0的认证了,因为自己用,所以删了很多容错的代码

     目前就写了下认证的,api调用的还没写,最近没时间,要赶紧训练了,不然就等虐吧……

文件位置:

/usr/share/gwibber/plugins/sina/gtk/sina

注:按ctrl+L就可以切换到路径编辑模式,Esc退出

from gi.repository import Gdk, Gtk, WebKit, Pango
from gi.repository.Gtk import Builder

import urllib, urllib2, json, urlparse, uuid,requests
from oauth import oauth

from gwibber.microblog.util import resources
from gwibber.microblog.util.keyring import get_from_keyring
import gettext
from gettext import gettext as _
if hasattr(gettext, 'bind_textdomain_codeset'):
    gettext.bind_textdomain_codeset('gwibber','UTF-8')
gettext.textdomain('gwibber-service-sina')
import sina.utils

Gdk.threads_init()

sigmeth = oauth.OAuthSignatureMethod_HMAC_SHA1()

class AccountWidget(Gtk.VBox):
  """AccountWidget: A widget that provides a user interface for configuring sina accounts in Gwibber
  """
  
  def __init__(self, account=None, dialog=None):
    """Creates the account pane for configuring Sina accounts"""
    Gtk.VBox.__init__( self, False, 20 )
    self.ui = Gtk.Builder()
    self.ui.set_translation_domain ("gwibber")
    self.ui.add_from_file (resources.get_ui_asset("gwibber-accounts-sina.ui"))
    self.ui.connect_signals(self)
    self.vbox_settings = self.ui.get_object("vbox_settings")
    self.pack_start(self.vbox_settings, False, False, 0)
    self.show_all()

    self.account = account or {}
    self.dialog = dialog
    self.window = dialog.dialog
    has_access_token = False

    has_secret_key = False
    if self.account.has_key("id"):
      has_secret_key = get_from_keyring(self.account['id'], 'secret_token') is not None


    try:
      if self.account.has_key("access_token") and  self.account.has_key("username") and has_secret_key and not self.dialog.condition:
        self.ui.get_object("hbox_sina_auth").hide()
        self.ui.get_object("sina_auth_done_label").set_label(_("%s has been authorized by Sina") % self.account["username"])
        self.ui.get_object("hbox_sina_auth_done").show()
      else:
        self.ui.get_object("hbox_sina_auth_done").hide()
        if self.dialog.ui:
          self.dialog.ui.get_object('vbox_create').hide()
    except:
      self.ui.get_object("hbox_sina_auth_done").hide()
      if self.dialog.ui:
        self.dialog.ui.get_object("vbox_create").hide()
  def on_sina_auth_clicked(self, widget, data=None):
    self.winsize = self.window.get_size()

    web = WebKit.WebView()
    web.get_settings().set_property("enable-plugins", False)
    web.get_settings().set_property("enable-developer-extras", False)
    web.load_html_string(_("<p>Please wait...</p>"), "file:///")

    self.consumer = oauth.OAuthConsumer(*sina.utils.get_sina_keys())

    url ="https://api.weibo.com/oauth2/authorize?client_id=%s&response_type=code&redirect_uri=http://gwibber.com/0/auth.html"%self.consumer.key
    web.load_uri(url)
    web.set_size_request(550, 400)
    web.connect("title-changed", self.on_sina_auth_title_change)

    self.scroll = Gtk.ScrolledWindow()
    
    self.scroll.add(web)
    self.scroll.set_size_request(550, 400)

    self.pack_start(self.scroll, True, True, 0)
    self.show_all()
    self.dialog.infobar.hide()

    self.ui.get_object("vbox1").hide()
    self.ui.get_object("vbox_advanced").hide()
    self.dialog.infobar.set_message_type(Gtk.MessageType.INFO)

  def on_sina_auth_title_change(self, web=None, title=None, data=None):
    saved = False
    if title.get_title() == "Success":

      if hasattr(self.dialog, "infobar_content_area"):
        for child in self.dialog.infobar_content_area.get_children(): child.destroy()
      self.dialog.infobar_content_area = self.dialog.infobar.get_content_area()
      self.dialog.infobar_content_area.show()
      self.dialog.infobar.show()
      url = web.get_main_frame().get_uri()
      code = url.split("=")[1]

      message_label = Gtk.Label(_("Verifying"))
      message_label.set_use_markup(True)
      message_label.set_ellipsize(Pango.EllipsizeMode.END)
      self.dialog.infobar_content_area.add(message_label)
      self.dialog.infobar.show_all()
      self.scroll.destroy()

#      self.ui.get_object("vbox1").show()
 #     self.ui.get_object("vbox_advanced").show()
     
      request=requests.session()

      data=request.post("https://api.weibo.com/oauth2/access_token?client_id=%s&client_secret=%s&grant_type=authorization_code&redirect_uri=http://gwibber.com/0/auth.html&code=%s"%(self.consumer.key,self.consumer.secret,code)).content
      data = json.loads(data)
      self.account["user_id"] = data["uid"]
      self.account["access_token"] = data["access_token"]
      s=request.get('https://api.weibo.com/2/users/show.json?access_token=%s&uid=%s'%(data['access_token'],data['uid']))
      account_data=json.loads(s.content)
      self.account['username']=account_data['screen_name'].encode('utf-8')
      self.dialog.on_edit_account_save()
    else:
      self.dialog.infobar.set_message_type(Gtk.MessageType.ERROR)
      message_label = Gtk.Label (_("Authorization failed. Please try again."))
      message_label.set_use_markup(True)
      message_label.set_ellipsize(Pango.EllipsizeMode.END)
      self.dialog.infobar_content_area.add(message_label)
      self.dialog.infobar.show_all()

      self.ui.get_object("vbox1").show()
      self.ui.get_object("vbox_advanced").show()
      self.scroll.destroy()
      self.window.resize(*self.winsize)
      self.dialog.select_account ()


 类似资料: