Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Wechat 中client()的access_token一直为nil,导致每次请求微信api会重新申请access_token #23

Open
taverngoal opened this issue Jul 1, 2020 · 1 comment

Comments

@taverngoal
Copy link

版本: 0.4.8

config.exs:

config :wechat,
  adapter_opts: {Wechat.Adapters.Sandbox, []},
  httpoison_opts: [recv_timeout: 300_000]

demo:

defmodule MyApp.Wechat do
  use Wechat, otp_app: :my_app

  def users do
    client() |> Wechat.User.get()
  end
end

若使用 MyApp.Wechat.client()会输出:

%Wechat.Client{
  access_token: nil,
  appid: "wx00000000000",
  encoding_aes_key: nil,
  endpoint: "http://************",
  secret: "***********************",
  token: nil
}

access_token一直为nil

每次请求微信的api, MyApp.Wechat.access_token 每次都不同,会消耗完access_token的限制次数。


看过源码后,加入了Map.put(:access_token, access_token())就能解决access_token每次都需要重新获取的问题。

实现:

defmodule BeautyMotherland.Wechat do
  use Wechat, otp_app: :beauty_motherland

  def users do
    client()
    |> Map.put(:access_token, access_token())
    |> Wechat.User.get()
  end

  def sign_jsapi(url) do
    client()
    |> Map.put(:access_token, access_token())
    |> Wechat.Client.sign_jsapi(url)
  end

  def jssdk(url, js_api_list \\ []) do
    %{timestamp: timestamp, noncestr: nonce, signature: signature} = sign_jsapi(url)

    %{
      jsApiList: js_api_list,
      appId: client().appid,
      timestamp: timestamp,
      nonceStr: nonce,
      signature: signature
    }
  end

  def user_info(openid) do
    client()
    |> Map.put(:access_token, access_token())
    |> Wechat.User.info(openid)
  end
end
@goofansu
Copy link
Collaborator

goofansu commented Jul 5, 2020

多谢反馈,我检查一下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants