dounaidu0204 2015-03-23 08:45
浏览 555

将refresh_token交换为access_token的最佳方法是什么? (Google API)

I make many different api calls to google api using access_token so I need to handle case when access_token is expired and exchange refresh_token to get new one.

What is the best way to do this?

I guess turn every API call with try{}catch(){} block.

If an exception rise, in catch() block I will make another API call to validate access_token through https://www.googleapis.com/oauth2/v1/tokeninfo?access_token={accessToken} endpoint.

This is probably not a good idea to turn every API call to try() catch(){} block and duplicate catch () logic.

How can i do it better?

P.S. I am using guzzle library for making API calls.

  • 写回答

4条回答 默认 最新

  • dowe98261 2015-03-23 09:09
    关注

    If you're using a library provided by Google to make your calls, then I suggest you check if the library does not already handle the retry for you. The Java library does this and I suspect the Python library also does.

    If you're not using a library, check if there would not be one available for your language. If will greatly help you.

    If you really don't have a choice and can't use Google's libraries, then I suggest you mimic their behavior in your own code. This would mean creating a kind of GoogleRequest behavior (a class, a module or something similar depending on your language). This behavior would provide an execute() method that would handle the authentication error, refresh the token and retry the request.

    By the way the URL used to refresh a token is not the one you listed in your question. You must send a POST request to the URL https://www.googleapis.com/oauth2/v3/token with this body :

    client_secret=YOUR_CLIENT_SECRET&grant_type=refresh_token&refresh_token=YOUR_REFRESH_TOKEN&client_id=YOUR-CLIENT_ID
    
    评论

报告相同问题?

悬赏问题

  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏