如何以编程方式刷新Google OAuth2令牌

时间:2016-11-21 11:56:53

标签: python django google-oauth2 python-social-auth

我正在使用Django与python-social-auth通过Google OAuth2对我的用户进行身份验证并使用Youtube API。

我想要达到的目标是让他们只登录并授权一次,并且只要我愿意就可以使用他们的凭据。这是不可能的,因为大约一个小时后它们就会过期,我需要用户重新授权我的应用程序获取新的凭据。

我认为解决方案是定期更新凭证,但我找不到任何可行的指南来解决这个问题。

我目前的配置是:

SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = 'the_key'
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = 'the_secret'
SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = ["https://www.googleapis.com/auth/youtube.readonly", "https://www.googleapis.com/auth/yt-analytics.readonly"]
SOCIAL_AUTH_GOOGLE_OAUTH2_AUTH_EXTRA_ARGUMENTS = {
   'access_type': 'offline',
   'approval_prompt': 'force'
}

此外,授权用户还有以下内容:

{
  "access_token": "the_access_token",
  "expires": 3600,
  "refresh_token": "the_refresh_token",
  "token_type": "Bearer"
}

我尝试使用下面的代码刷新它但是因为它仍然过期我认为这不是正确的方法:

social_user = UserSocialAuth.objects.get(...)  # Fetch the instance
social_user.refresh_token(load_strategy())  # Returns "None"

1 个答案:

答案 0 :(得分:0)

经过一些测试后我发现使用 <!DOCTYPE html> <head> <title>A7X</title> <meta charset="utf-8"> <link rel="stylesheet" href="a7x.css" type="text/css"> <link href="https://fonts.googleapis.com/css?family=Cormorant+Garamond|Ravi+Prakash" rel="stylesheet"> </head> <body> <h1 class="hide">Avenged Sevenfold lyrics</h1> <header><h1>We have one collective hope: The Earth</h1><img src="../bilder/avenged_sevenfold___logo__1__png__deathbat_by_lightsinaugust-d5klaeb.png" alt="Avenged Sevenfolds logo"></header> <nav> <p>Album Search</p> <form action="" onsubmit="return sendToPage()" method="post"> <input id="search" type="text" name="text" placeholder="Album name"> <input type="submit" value="Submit"> </form> </nav> <main> <div class="wrapper"> <div class="ram" id="Stage"> <details> <summary>Click here to make a song selection!</summary> <li>The Stage</li> <li>Paradigm</li> <li>Sunny Disposition</li> <li>God Damn</li> <li>Creating God</li> <li>Angels</li> <li>Simulation</li> <li>Higher</li> <li>Roman Sky</li> <li>Fermi Paradox</li> <li>Exist</li> </details> <img src="../bilder/The-Stage-Album-Cover-sept8_CMYK.jpg" alt="The Stage album"> </div> <div class="ram" id="Hail"> <details> <summary>Click here to make a song selection!</summary> <li>Shepherd of Fire</li> <li>Hail to the King</li> <li>Doing Time</li> <li>This Means War</li> <li>Requiem</li> <li>Crimson Day</li> <li>Heretic</li> <li>Coming Home</li> <li>Planets</li> <li>Acid Rain</li> <li>St. James</li> </details> <img src="../bilder/Hail-To-The-King.jpg" alt="Hail To The King Album"> </div> <div class="ram" id="Nightmare"> <details> <summary>Click here to make a song selection!</summary> <li>Nightmare</li> <li>Welcome to the Family</li> <li>Danger Line</li> <li>Buried Alive</li> <li>Natural Born Killer</li> <li>So Far Away</li> <li>God Hates Us</li> <li>Victim</li> <li>Tonight the World Dies</li> <li>Fiction</li> <li>Save Me</li> <li>Lost It All</li> </details> <img src="../bilder/Nightmare.jpg" alt="Nightmare Album"> </div> <div class="ram" id="Self"> <details> <summary>Click here to make a song selection!</summary> <li>Critical Acclaim</li> <li>Almost Easy</li> <li>Scream</li> <li>Afterlife</li> <li>Gunslinger</li> <li>Unbound (The Wild Ride)</li> <li>Brompton Cocktail</li> <li>Lost</li> <li>A Little Piece of Heaven</li> <li>Dear God</li> <li>Crossroads</li> </details> <img src="../bilder/Avenged-Sevenfold.jpg" alt="Avenged Sevenfold Album"> </div> <div class="ram" id="City"> <details> <summary>Click here to make a song selection!</summary> <li>Beast and the Harlot</li> <li>Burn It Down</li> <li>Blinded in Chains</li> <li>Bat Country</li> <li>Trashed and Scattered</li> <li>Seize the Day</li> <li>Sidewinder</li> <li>The Wicked End</li> <li>Strength of the World</li> <li>Betrayed</li> <li>M.I.A</li> </details> <img src="../bilder/City-Of-Evil.jpg" alt="City of Evil Album"> </div> <div class="ram" id="Waking"> <details> <summary>Click here to make a song selection!</summary> <li>Waking the Fallen</li> <li>Unholy Confessions</li> <li>Chapter Four</li> <li>Remenissions</li> <li>Desecrate Through Reverence</li> <li>Eternal Rest</li> <li>Second Heartbeat</li> <li>Radiant Eclipse</li> <li>I Won't See You Tonight (Part 1)</li> <li>I Won't See You Tonight (Part 2)</li> <li>Clairvoyant Disease</li> <li>And All Things Will End</li> </details> <img src="../bilder/Waking-The-Fallen.jpg" alt="Waking the Fallen Album"> </div> <div class="ram" id="Sound"> <details> <summary>Click here to make a song selection!</summary> <li>To End the Rapture</li> <li>Turn the Other Way</li> <li>Darkness Surrounding</li> <li>The Art of Subconscious Illusion</li> <li>We Come Out at Night</li> <li>Lips of Deceit</li> <li>Warmness on the Soul</li> <li>An Epic of Time Wasted</li> <li>Breaking Their Hold</li> <li>Forgotten Faces</li> <li>Thick and Thin</li> <li>Streets</li> <li>Shattered by Broken Dreams</li> </details> <img src="../bilder/Sounding-The-Seventh-Trumpet.jpg" alt="Sounding the Seventh Trumpet Album"> </div> </div> </main> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script src="animation.js"></script> </div> </body> </html> **Javascript** function main() { var $body = $("body").hide(); $("body").fadeIn(1000); $('summary').each(function(){ $(this).nextAll().wrapAll('<div class="slide"></div>'); }); $('details').attr('open','').find('.slide').css('display','none'); $('summary').click(function(e) { e.preventDefault(); $(this).siblings('div.slide').slideToggle(function(){ $(this).parent('details').toggleClass('open');}); }); } $(document).ready(main); function sendToPage(){ var input = document.getElementById("search").value; switch(input){ case "nightmare": location.replace("#Nightmare"); break; case "the stage": location.replace("#Hail"); break; case "self": location.replace("#Self"); break; case "city": location.replace("#City"); break; case "waking": location.replace("#Waking"); break; case "sound": location.replace("#Sound"); break; default: alert("No matching albums, try again!"); location.replace("file:///C:/inetpub/wwwroot/projektbrackets/AvengedSevenfold/index.html"); break; } } **CSS** * > { box-sizing: border-box; } body { margin: 0; padding: 0; width: 100%; background-color: #2c382d; font-size: 130%; } header img { display: block; margin: auto; max-width: 30%; min-width: 25%; border: none; } input { width: 100%; } nav { width: 159px; margin: 0 auto; text-align: center; min-width: 404px; color: white; } header { background-color: #647155; text-align: center; font-family: 'Cormorant Garamond', serif; } .ram img { width: 300px; height: auto; display: block; } .wrapper { max-width: 100vw; min-width: 388px; display: flex; flex-wrap: wrap; justify-content: space-around; margin-top: 40px; align-items: flex-start; } .ram { margin: 10px 0; } input { width: 155px; } summary { background-color: black; color: white; padding: 4px; border: 1px blue solid; border-radius: 3px; text-align: center; font-size: 89%; } li { background-color: #212121; color: white; list-style-type: none; text-align: center; margin-right: 5px; font-family: 'Ravi Prakash', cursive; } header { min-width: 420px; } .hide { position: absolute !important; top: -9999px !important; left: -9999px !important; } h1 { margin: 0; } summary::-webkit-details-marker { display: none; } summary:before { content: "►"; } details.open summary:before { content: "▼"; } li:nth-child(odd){background-color: crimson;} 实际上会刷新它再使用一小时,所以我需要做一个cron作业来运行Django命令在到期前刷新它们,如下所示:

refresh_token

我真正想要的是一种能够按需刷新/拥有令牌的方式,就像其他社交网络提供的那样(Instagram,Twitter,Facebook)只需要一次登录且凭据永不过期。