{"id":205337,"date":"2025-05-29T15:58:22","date_gmt":"2025-05-29T07:58:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205337\/"},"modified":"2025-05-29T15:58:22","modified_gmt":"2025-05-29T07:58:22","slug":"%e5%b0%86-google-%e6%97%a5%e5%8e%86%e8%bf%9e%e6%8e%a5%e5%88%b0-django-%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205337\/","title":{"rendered":"\u5c06 Google \u65e5\u5386\u8fde\u63a5\u5230 Django \u5e94\u7528\u7a0b\u5e8f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5c06 Google \u65e5\u5386\u8fde\u63a5\u5230 Django \u5e94\u7528\u7a0b\u5e8f<\/h1>\n<p>\u6765\u5230\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300a\u5c06 Google \u65e5\u5386\u8fde\u63a5\u5230 Django \u5e94\u7528\u7a0b\u5e8f\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241129\/173285642867494a6c2800b.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5c06 google \u65e5\u5386\u4e0e django \u5e94\u7528\u7a0b\u5e8f\u65e0\u7f1d\u96c6\u6210\u4ee5\u589e\u5f3a\u65e5\u7a0b\u5b89\u6392\u548c\u4e8b\u4ef6\u7ba1\u7406\u7684\u5206\u6b65\u6307\u5357\u3002<\/p>\n<p>\u5c06 google \u65e5\u5386\u4e0e django \u5e94\u7528\u7a0b\u5e8f\u96c6\u6210\u53ef\u4ee5\u901a\u8fc7\u542f\u7528\u65e5\u7a0b\u5b89\u6392\u3001\u4e8b\u4ef6\u7ba1\u7406\u548c\u65e5\u5386\u540c\u6b65\u6765\u663e\u7740\u589e\u5f3a web \u5e94\u7528\u7a0b\u5e8f\u7684\u529f\u80fd\u3002\u672c\u6307\u5357\u5c06\u5f15\u5bfc\u60a8\u5b8c\u6210\u5c06 google \u65e5\u5386\u8fde\u63a5\u5230 django \u5e94\u7528\u7a0b\u5e8f\u7684\u6b65\u9aa4\uff0c\u6db5\u76d6\u4ece\u8bbe\u7f6e google api \u51ed\u636e\u5230\u5728 django \u4e2d\u5b9e\u73b0\u5fc5\u8981\u4ee3\u7801\u7684\u6240\u6709\u5185\u5bb9\u3002<\/p>\n<p>\u5f00\u59cb\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u60a8\u5177\u5907\u4ee5\u4e0b\u6761\u4ef6\uff1a<\/p>\n<p><strong>1. django \u5e94\u7528\u7a0b\u5e8f\uff1a<\/strong> \u4e00\u4e2a\u5de5\u4f5c\u7684 django \u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p><strong>2. google api \u63a7\u5236\u53f0\u5e10\u6237\uff1a<\/strong> \u8bbf\u95ee google cloud console\u3002<\/p>\n<p><strong>3.\u5df2\u542f\u7528 google calendar api\uff1a<\/strong> \u5e94\u5728 google cloud console \u4e2d\u4e3a\u60a8\u7684\u9879\u76ee\u542f\u7528 google calendar api\u3002<\/p>\n<hr>\n<p><strong>1.\u521b\u5efa\u9879\u76ee\uff1a<\/strong><br \/> \u8f6c\u5230 google cloud console \u5e76\u521b\u5efa\u4e00\u4e2a\u65b0\u9879\u76ee\u3002<\/p>\n<p><strong>2.\u542f\u7528 google \u65e5\u5386 api\uff1a<\/strong><br \/> \u5bfc\u822a\u81f3\u201capi &amp; services\u201d &gt; \u201clibrary\u201d \u5e76\u641c\u7d22\u201cgoogle calendar api\u201d\u3002\u4e3a\u60a8\u7684\u9879\u76ee\u542f\u7528\u5b83\u3002<\/p>\n<p><strong>3.\u914d\u7f6e\u540c\u610f\u5c4f\u5e55\uff1a<\/strong><\/p>\n<ul>\n<li>\u5bfc\u822a\u81f3\u201capi &amp; services\u201d &gt; \u201coauth \u540c\u610f\u5c4f\u5e55\u201d\u5e76\u914d\u7f6e\u540c\u610f\u5c4f\u5e55\u3002<\/li>\n<li>\u73b0\u5728\u9009\u62e9\u60a8\u60f3\u8981\u7684 oauth \u7c7b\u578b\uff08\u5728\u672c\u4f8b\u4e2d\u4e3a\u5916\u90e8\uff0c\u56e0\u4e3a\u62e5\u6709 google \u5e10\u6237\u7684\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u8bbf\u95ee\u8be5\u5e94\u7528\u7a0b\u5e8f\uff09\u3002<\/li>\n<li>\u6839\u636e\u9700\u8981\u8bbe\u7f6e\u540c\u610f\u5c4f\u5e55\u7684\u6240\u6709\u6570\u636e\uff0c\u5982\u5e94\u7528\u7a0b\u5e8f\u540d\u79f0\u3001\u5fbd\u6807\u3001\u652f\u6301\u7535\u5b50\u90ae\u4ef6\u7b49\u3002<\/li>\n<li>\u70b9\u51fb\u201c\u6dfb\u52a0\u6216\u5220\u9664\u8303\u56f4\u201d\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u8303\u56f4\uff0c\u2026\/auth\/userinfo.email\u3001\u2026\/auth\/userinfo.profile\u3001openid \u6765\u8bbf\u95ee\u7528\u6237\u4fe1\u606f\u4ee5\u53ca\u6240\u6709 google calendar api \u8303\u56f4\u6765\u8bbf\u95ee\u7528\u6237\u7684 google \u65e5\u5386\u3002\u7136\u540e\u70b9\u51fb\u66f4\u65b0\u4fdd\u5b58\u3002<\/li>\n<li>\u4e0b\u4e00\u6b65\u6dfb\u52a0\u6d4b\u8bd5\u7528\u6237\u3002\u7531\u4e8e\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u5c1a\u672a\u7ecf\u8fc7\u8c37\u6b4c\u9a8c\u8bc1\uff0c\u56e0\u6b64\u53ea\u6709\u6b64\u5217\u8868\u4e2d\u7684\u7528\u6237\u624d\u80fd\u6ce8\u518c\u5230\u6b64\u8c37\u6b4c\u9879\u76ee\u3002\u56e0\u6b64\uff0c\u8bf7\u6dfb\u52a0\u60a8\u5c06\u7528\u4e8e\u6d4b\u8bd5 google \u65e5\u5386\u96c6\u6210\u7684\u6240\u6709\u6d4b\u8bd5\u7535\u5b50\u90ae\u4ef6\u3002 \u5b8c\u6210\u540e\u7ee7\u7eed\u521b\u5efa\u51ed\u636e\u3002<\/li>\n<\/ul>\n<p><strong>4.\u521b\u5efa oauth \u51ed\u636e\uff1a<\/strong><br \/> \u8f6c\u5230\u201capi &amp; services\u201d &gt; \u201ccredentials\u201d \u5e76\u521b\u5efa\u51ed\u636e\u3002\u9009\u62e9 oauth \u5ba2\u6237\u7aef id \u4f5c\u4e3a\u51ed\u636e\u7c7b\u578b\u3002\u5c06web\u5e94\u7528\u7a0b\u5e8f\u8bbe\u7f6e\u4e3a\u5e94\u7528\u7a0b\u5e8f\u7c7b\u578b\u5e76\u586b\u5199\u5e94\u7528\u7a0b\u5e8f\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<ul>\n<li>\u6388\u6743\u91cd\u5b9a\u5411 uri\uff1a\u6dfb\u52a0 django \u5e94\u7528\u7a0b\u5e8f\u7684\u91cd\u5b9a\u5411 url\uff08\u4f8b\u5982\uff0c\u7528\u4e8e\u672c\u5730\u5f00\u53d1\u7684 http:\/\/localhost:8000\/oauth2callback\uff09\u3002<\/li>\n<\/ul>\n<p><strong>5.\u4e0b\u8f7d json \u51ed\u8bc1\uff1a<\/strong><br \/> \u4e0b\u8f7d oauth 2.0 \u51ed\u636e json \u6587\u4ef6\u5e76\u59a5\u5584\u4fdd\u7ba1\u3002\u8be5\u6587\u4ef6\u5305\u542b\u60a8\u7684 client_id\u3001client_secret \u548c\u5176\u4ed6\u91cd\u8981\u4fe1\u606f\u3002<\/p>\n<hr>\n<p>\u60a8\u9700\u8981\u4e00\u4e9b python \u5305\u6765\u4e0e google api \u4ea4\u4e92\uff1a<\/p>\n<pre>pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client\n<\/pre>\n<hr>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u5185\u5bb9\u66f4\u65b0\u60a8\u7684 settings.py\uff1a<\/p>\n<pre>import os\n\n# google calendar api\ngoogle_client_secrets_file = os.path.join(base_dir, 'path\/to\/client_secret.json')\ngoogle_api_scopes = ['https:\/\/www.googleapis.com\/auth\/calendar']\nredirect_uri = 'http:\/\/localhost:8000\/oauth2callback'  # or your production url\n<\/pre>\n<hr>\n<p>\u521b\u5efa\u4e00\u4e2a\u89c6\u56fe\u6765\u5904\u7406 oauth2 \u6d41\u7a0b\uff1a<\/p>\n<pre>from google.oauth2.credentials import credentials\nfrom google_auth_oauthlib.flow import flow\nfrom django.shortcuts import redirect\nfrom django.http import httpresponse\nfrom django.conf import settings\n\ndef google_calendar_init(request):\n    flow = flow.from_client_secrets_file(\n        settings.google_client_secrets_file,\n        scopes=settings.google_api_scopes,\n        redirect_uri=settings.redirect_uri\n    )\n    authorization_url, state = flow.authorization_url(\n        access_type='offline',\n        include_granted_scopes='true'\n    )\n\n    request.session['state'] = state\n    return redirect(authorization_url)\n\ndef google_calendar_redirect(request):\n    state = request.session['state']\n\n    flow = flow.from_client_secrets_file(\n        settings.google_client_secrets_file,\n        scopes=settings.google_api_scopes,\n        state=state,\n        redirect_uri=settings.redirect_uri\n    )\n\n    flow.fetch_token(authorization_response=request.build_absolute_uri())\n\n    credentials = flow.credentials\n    request.session['credentials'] = credentials_to_dict(credentials)\n\n    return httpresponse('calendar integration complete. you can now use google calendar with your django app.')\n\ndef credentials_to_dict(credentials):\n    return {'token': credentials.token,\n            'refresh_token': credentials.refresh_token,\n            'token_uri': credentials.token_uri,\n            'client_id': credentials.client_id,\n            'client_secret': credentials.client_secret,\n            'scopes': credentials.scopes}\n<\/pre>\n<hr>\n<p>oauth2 \u6d41\u7a0b\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u5411 google calendar api \u53d1\u51fa\u7ecf\u8fc7\u8eab\u4efd\u9a8c\u8bc1\u7684\u8bf7\u6c42\u3002\u8fd9\u662f\u4e00\u4e2a\u5217\u51fa\u7528\u6237\u65e5\u5386\u4e8b\u4ef6\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<pre>from googleapiclient.discovery import build\n\ndef list_events(request):\n    credentials = credentials(**request.session['credentials'])\n    service = build('calendar', 'v3', credentials=credentials)\n\n    events_result = service.events().list(calendarid='primary', maxresults=10).execute()\n    events = events_result.get('items', [])\n\n    return httpresponse(events)\n<\/pre>\n<hr>\n<p>\u5728 urls.py \u4e2d\u6dfb\u52a0\u89c6\u56fe\u7684 url\uff1a<\/p>\n<pre>from django.urls import path\nfrom . import views\n\nurlpatterns = [\n    path('google-calendar\/init\/', views.google_calendar_init, name='google_calendar_init'),\n    path('oauth2callback\/', views.google_calendar_redirect, name='google_calendar_redirect'),\n    path('google-calendar\/events\/', views.list_events, name='list_events'),\n]\n<\/pre>\n<hr>\n<ol>\n<li>\n<p>\u542f\u52a8\u4f60\u7684 django \u670d\u52a1\u5668\uff1a<br \/> \u4f7f\u7528 python manage.py runserver \u8fd0\u884c django \u5f00\u53d1\u670d\u52a1\u5668\u3002<\/p>\n<\/li>\n<li>\n<p>\u9a8c\u8bc1\uff1a<br \/> \u5728\u6d4f\u89c8\u5668\u4e2d\u5bfc\u822a\u81f3 \/google-calendar\/init\/\u3002\u60a8\u5c06\u88ab\u91cd\u5b9a\u5411\u5230 google \u7684 oauth2 \u540c\u610f\u9875\u9762\u3002<\/p>\n<\/li>\n<li>\n<p>\u8bbf\u95ee\u6d3b\u52a8\uff1a<br \/> \u8eab\u4efd\u9a8c\u8bc1\u540e\uff0c\u524d\u5f80 \/google-calendar\/events\/ \u67e5\u770b\u60a8\u7684 google \u65e5\u5386\u6d3b\u52a8\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u5c06 google \u65e5\u5386\u4e0e django \u5e94\u7528\u7a0b\u5e8f\u96c6\u6210\uff0c\u8ba9\u60a8\u53ef\u4ee5\u76f4\u63a5\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6784\u5efa\u5f3a\u5927\u7684\u65e5\u7a0b\u5b89\u6392\u529f\u80fd\u3002\u901a\u8fc7\u9075\u5faa\u672c\u6307\u5357\uff0c\u60a8\u5df2\u7ecf\u8bbe\u7f6e\u4e86 oauth2 \u8eab\u4efd\u9a8c\u8bc1\u3001\u8fde\u63a5\u5230 google calendar api \u5e76\u83b7\u53d6\u4e86\u65e5\u5386\u4e8b\u4ef6\u3002\u60a8\u73b0\u5728\u53ef\u4ee5\u6839\u636e\u9700\u8981\u6269\u5c55\u6b64\u96c6\u6210\u4ee5\u5305\u62ec\u4e8b\u4ef6\u521b\u5efa\u3001\u66f4\u65b0\u548c\u5176\u4ed6\u65e5\u5386\u7ba1\u7406\u529f\u80fd\u3002<\/p>\n<p>ps\uff1a\u8bf7\u8bb0\u4f4f\u5b89\u5168\u5730\u5904\u7406\u51ed\u636e\u5e76\u786e\u4fdd\u6b63\u786e\u7684\u9519\u8bef\u5904\u7406\u4ee5\u5b9e\u73b0\u5f3a\u5927\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5c06 Google \u65e5\u5386\u8fde\u63a5\u5230 Django \u5e94\u7528\u7a0b\u5e8f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5c06 Google \u65e5\u5386\u8fde\u63a5\u5230 D&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205337","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205337","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205337"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205337\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}