小编bje*_*lli的帖子

Google API:使用oauth2client.client从刷新令牌获取凭据

我正在使用googles官方oauth2client.client访问google plus api.我有一个存储在数据库中的刷新令牌(不会过期),需要从中重新创建临时"凭据"(访问令牌).

但我无法找到一种方法来与谷歌提供的官方图书馆这样做.

所以我讨厌它:使用urllib访问API,从refresh_token给我一个新的access_token.使用access_token我可以使用该库.

我一定是想念一下!

from apiclient import discovery
from oauth2client.client import AccessTokenCredentials
from urllib import urlencode
from urllib2 import Request , urlopen, HTTPError
import json

# ==========================================

def access_token_from_refresh_token(client_id, client_secret, refresh_token):
  request = Request('https://accounts.google.com/o/oauth2/token',
    data=urlencode({
      'grant_type':    'refresh_token',
      'client_id':     client_id,
      'client_secret': client_secret,
      'refresh_token': refresh_token
    }),
    headers={
      'Content-Type': 'application/x-www-form-urlencoded',
      'Accept': 'application/json'
    }
  )
  response = json.load(urlopen(request))
  return response['access_token']

# ==========================================

access_token = access_token_from_refresh_token(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN)

# now I can use the library properly
credentials = AccessTokenCredentials(access_token, "MyAgent/1.0", None)
http = …
Run Code Online (Sandbox Code Playgroud)

python google-api google-plus

16
推荐指数
4
解决办法
2万
查看次数

由于缺少公钥,使用 rvm 安装 ruby​​s 时出现问题

昨天我尝试在 Ubuntu 上安装 ruby​​ 或更新 rvm,今天在 mac os 上,但我在两个地方都失败了:

$ rvm get head
Downloading https://get.rvm.io
Downloading https://raw.githubusercontent.com/rvm/rvm/master/binscripts/rvm-installer.asc
Verifying /Users/bjelline/.rvm/archives/rvm-installer.asc
gpg: Signature made Sun Dec 30 11:44:46 2018 CET using RSA key ID 39499BDB
gpg: Can't check signature: No public key
Warning, RVM 1.26.0 introduces signed releases and automated check of signatures when GPG software found. Assuming you trust Michal Papis import the mpapis public key (downloading the signatures).

GPG signature verification failed for '/Users/bjelline/.rvm/archives/rvm-installer' - 'https://raw.githubusercontent.com/rvm/rvm/master/binscripts/rvm-installer.asc'! Try to install GPG …
Run Code Online (Sandbox Code Playgroud)

cryptography gnupg rvm

8
推荐指数
1
解决办法
2033
查看次数

Pandas,groupby并在组中找到最大值,返回值和计数

我有一个带有日志数据的pandas DataFrame:

        host service
0   this.com    mail
1   this.com    mail
2   this.com     web
3   that.com    mail
4  other.net    mail
5  other.net     web
6  other.net     web
Run Code Online (Sandbox Code Playgroud)

我想在每个主机上找到提供最多错误的服务:

        host service  no
0   this.com    mail   2
1   that.com    mail   1
2  other.net     web   2
Run Code Online (Sandbox Code Playgroud)

我找到的唯一解决方案是按主机和服务进行分组,然后迭代索引的0级.

谁能建议一个更好,更短的版本?没有迭代?

df = df_logfile.groupby(['host','service']).agg({'service':np.size})

df_count = pd.DataFrame()
df_count['host'] = df_logfile['host'].unique()
df_count['service']  = np.nan
df_count['no']    = np.nan

for h,data in df.groupby(level=0):
  i = data.idxmax()[0]   
  service = i[1]             
  no = data.xs(i)[0]
  df_count.loc[df_count['host'] == h, 'service'] = service
  df_count.loc[(df_count['host'] == h) …
Run Code Online (Sandbox Code Playgroud)

python numpy pandas

7
推荐指数
1
解决办法
9838
查看次数

标签 统计

python ×2

cryptography ×1

gnupg ×1

google-api ×1

google-plus ×1

numpy ×1

pandas ×1

rvm ×1