首页 \ 问答 \ 使用django-social-auth(omab)自动刷新访问令牌(Automatically refresh access token with django-social-auth (omab))

使用django-social-auth(omab)自动刷新访问令牌(Automatically refresh access token with django-social-auth (omab))

我目前正在使用django-social-auth管理oauth2注册google-oauth2以访问Google云端硬盘。 我已经添加了对extra_arguments的离线访问。 因此,Google会返回刷新令牌,并由django-social-auth存储。 问题是django-social-auth从不使用此刷新令牌来更新访问令牌。 因此,访问令牌在一小时后过期,我无法使用它来执行脱机请求。 我想保持access_token有效24/7有效,这样我就可以让我的数据库与每个用户Google Drive同步。

GOOGLE_OAUTH2_AUTH_EXTRA_ARGUMENTS = {'access_type':'offline'}
GOOGLE_OAUTH_EXTRA_SCOPE = ['https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.profile']

SOCIAL_AUTH_USER_MODEL = 'accounts.GoogleDriveUser'
SOCIAL_AUTH_EXTRA_DATA = True
SOCIAL_AUTH_SESSION_EXPIRATION = False

有没有办法强制django-social auth在每次使用refresh_token到期时更新access_token。 我很想看到如何解决这个问题的一个例子。


I am currently using django-social-auth to manage oauth2 registration with google-oauth2 for access to Google Drive. I have added offline access to my extra_arguments. Therefore Google returns a refresh token and it is stored by django-social-auth. The problem is that django-social-auth never uses this refresh token to update the access token. Therefore the access token expires after one hour, and I can't use it to perform offline requests. I want to keep the access_token valid 24/7 so I can keep my database synced with each users Google Drive.

GOOGLE_OAUTH2_AUTH_EXTRA_ARGUMENTS = {'access_type':'offline'}
GOOGLE_OAUTH_EXTRA_SCOPE = ['https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.profile']

SOCIAL_AUTH_USER_MODEL = 'accounts.GoogleDriveUser'
SOCIAL_AUTH_EXTRA_DATA = True
SOCIAL_AUTH_SESSION_EXPIRATION = False

Is there a way to force django-social auth to update the access_token every time it expires using the refresh_token. I would love to see an example of how this problem could be solved.


原文:https://stackoverflow.com/questions/12912685
更新时间:2023-07-19 21:07

最满意答案

代码有多个问题,我会用代码注释来解释它们

class foo1
{
    public:
        //use initializer lists to avoid extra copying
        foo1() : a("text") 
        {
        }

        //change return type from void to a const reference to string
        const string & getString()
        {
            return a;
        }
    private:
        string a;
};

class foo2
{
    public:
        //use initializer lists to avoid extra copying
        foo2() : boo3() 
        {
        }
        class foo3
        {
            public:
                //use initializer lists to avoid extra copying
                foo3() : boo1()
                {
                }

                //you need a function that will allow access to the private member. Returning a const reference avoids extra copying
                const foo1 & boo()
                {
                    return boo1;
                }

            private:
                foo1 boo1;
        };

        //you need a function that will allow access to the private member
        const foo3 & foo()
        {
            return boo3;
        }
    //you need to save the foo3 object in the class to be able to use it later
    private:
        foo3 boo3;
};

int main()
{
    foo2 object;
    cout << object.foo().boo().getString();
}

现在这是你如何访问字符串:

    cout << object.foo().boo().getString();
            \__________/ \___/ \_________/
                 ^         ^        ^---- get the string from the foo1 object
                 |         |---- get the foo1 object from the foo3 object
                 |---- get the foo3 object stored in "object"

There are multiple issues with the code, I'll explain them with code comments

class foo1
{
    public:
        //use initializer lists to avoid extra copying
        foo1() : a("text") 
        {
        }

        //change return type from void to a const reference to string
        const string & getString()
        {
            return a;
        }
    private:
        string a;
};

class foo2
{
    public:
        //use initializer lists to avoid extra copying
        foo2() : boo3() 
        {
        }
        class foo3
        {
            public:
                //use initializer lists to avoid extra copying
                foo3() : boo1()
                {
                }

                //you need a function that will allow access to the private member. Returning a const reference avoids extra copying
                const foo1 & boo()
                {
                    return boo1;
                }

            private:
                foo1 boo1;
        };

        //you need a function that will allow access to the private member
        const foo3 & foo()
        {
            return boo3;
        }
    //you need to save the foo3 object in the class to be able to use it later
    private:
        foo3 boo3;
};

int main()
{
    foo2 object;
    cout << object.foo().boo().getString();
}

Now this is how you access the string:

    cout << object.foo().boo().getString();
            \__________/ \___/ \_________/
                 ^         ^        ^---- get the string from the foo1 object
                 |         |---- get the foo1 object from the foo3 object
                 |---- get the foo3 object stored in "object"

相关问答

更多
  • “变量tempv和tempv在最外层for循环的每次迭代中都被重新声明 不,他们在每次迭代时都会被实例化 。 创建,并在块的末尾,销毁。 模块优化由编译器完成。 范围是编译时的概念。 通过检查代码看到的东西。 实例化是一种运行时效果。 可以多次执行相同的变量声明,实例化变量次数。 好吧,除非它是static ,在这种情况下它被实例化一次。 “我理解在C ++中,变量不能在同一范围内重新声明。 它不能在同一范围内直接重新声明,但可以在嵌套范围内重新声明。 而且你有许多嵌套的范围。 让我们考虑一下: for(i ...
  • 概要 感谢Jan对奇怪的重复模板模式的解释。 这是一个有趣的方法来了解。 我最终接受了自己的答案,因为我觉得它直接回答了问题。 CRTP方法很好,但不能直接回答问题1和2,但确实提供了一个很好的选择。 问题1: 看来这是可能的。 感谢mkirci和R. Martinho Fernandes确认我对编译器产生警告的原因以及忽视它是否“安全”的怀疑。 总结...不是在构造函数的初始化列表中使用this的最佳想法,因为尚未构造类。 正如大家指出的那样,如果使用指针,这可能会导致UB。 我决定使用我的工作,使用指向 ...
  • 错误:'Car'没有命名一个类型 在...处 static Car* createCar() 汽车还不知道。 在功能上移动Class Car的定义 错误:'string'没有命名一个类型在函数'void useVehicle()'中: #include 也使用std::来限定string 错误:'createCar'不是'Vehicle'的成员 解决其他两个问题后,此错误将消失。 编译器无法解析函数声明,因为它不知道它的返回类型是什么。 error: ‘Car’ does not na ...
  • 代码有多个问题,我会用代码注释来解释它们 class foo1 { public: //use initializer lists to avoid extra copying foo1() : a("text") { } //change return type from void to a const reference to string const string & getString() ...
  • 问题是在调用ABa不会构造类模板( A )。 也就是说, A还没有绑定到一个类。 试试这个解决方法: class A(): class B(): a = 1 现在单独创建C (已定义A ): class C(): b = 2 c = A.B.a + b 并参考A C : A.C = C 这可以通过元类来完成,但在这里可能是过度杀戮。 The problem is that the class template (A) is not constructed whi ...
  • 像这样的东西: class Plass { public: Plass(Point *newPoint, Way *newWay) { moint = newPoint; bay = newWay; // or instantiate here: // moint = new Point(); // bay = new Way(); // just don't forget to me ...
  • 这是因为在java中我们有内部类 ,而在c#中我们有嵌套类 。 内部类具有对外部类的隐式引用,从而可以从内部类调用该类中的方法。 嵌套类只包含该类,没有对“外部”类的引用。 例如,如果我们有两个“外部”类的实例,你会调用哪种方法呢? 要在C#中获得大致相同的东西,你会做这样的事情 class Outer { private void foo() {} class Nested { private readonly Outer _outer; publi ...
  • 对于数字2这取决于许多事情,包括编译器和平台。 基本上,向函数传递和返回参数的不同方式称为调用约定 。 文章在x86平台上调用约定详细介绍了操作顺序,你可以看到它只是这个平台和编译器的小组合有多么丑陋和复杂,这很可能是你听到各种不同场景的原因。关于函数调用约定的gen。 涵盖了更广泛的场景,包括64 bit平台,但更难阅读。 它变得更加复杂,因为gcc实际上可能不会push和pop堆栈而是直接操作堆栈指针,我们可以看到这样的一个例子,尽管在这里是汇编。 关于调用约定很难概括,如果参数的数量足够小,许多调用约 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)