使用 google oauth2 时状态码 405
Status Code 405 while using google oauth2
我正在使用带有 Angular JS 的 Django 来访问 Google 驱动器 API。我正在关注来自 Google 的 this 文档。 FLOW.step1_get_authorize_url()
给我的 URL 类似于页面上提到的示例 URL。但问题是在 return HttpResponseRedirect(authorize_url)
之后浏览器不会重定向到 authorize_url
并给出如下图所示的错误 (Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8000' is therefore not allowed access. The response had HTTP status code 405
).
但是如果我复制粘贴 URL,它工作正常。
oauth2 函数如下所示。
def index(request):
FLOW = flow_from_clientsecrets(
settings.GOOGLE_OAUTH2_CLIENT_SECRETS_JSON,
scope='https://www.googleapis.com/auth/drive',
redirect_uri='http://127.0.0.1:8000/oauth2callback/'
)
FLOW.params['access_type'] = 'offline'
authorize_url = FLOW.step1_get_authorize_url()
return HttpResponseRedirect(authorize_url)
这里是 oauth2callback 函数。
def auth_return(request):
credential = FLOW.step2_exchange(request.GET)
return HttpResponseRedirect("/mycustomurl")
我使用 this 在 Django 服务器端启用 CORS。这是我在 Angular 中调用 oauth2 的服务部分。
(function () {
'use strict';
angular.module('myApp')
.service('myService', function ($http) {
this.saveToDrive = function (startYear, endYear, shape) {
var config = {
params: {
start: '1999',
end: '2002',
action: 'download-to-drive'
},
headers: {
'Access-Control-Allow-Origin': '*',
'X-Requested-With': null
}
}
var promise = $http.get('/oauth2/', config)
.then(function (response) {
return response.data;
});
return promise;
};
});
})();
请提出我在这里遗漏了什么。非常感谢任何帮助或建议。
我发现这是一个小设计问题,而不是代码问题。我分离了向客户端发送 oauth2 请求的逻辑,并且在 oauth2 请求之后,我使用 params
选项向内部 API 发送了请求。现在一切正常。
我正在使用带有 Angular JS 的 Django 来访问 Google 驱动器 API。我正在关注来自 Google 的 this 文档。 FLOW.step1_get_authorize_url()
给我的 URL 类似于页面上提到的示例 URL。但问题是在 return HttpResponseRedirect(authorize_url)
之后浏览器不会重定向到 authorize_url
并给出如下图所示的错误 (Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8000' is therefore not allowed access. The response had HTTP status code 405
).
但是如果我复制粘贴 URL,它工作正常。
oauth2 函数如下所示。
def index(request):
FLOW = flow_from_clientsecrets(
settings.GOOGLE_OAUTH2_CLIENT_SECRETS_JSON,
scope='https://www.googleapis.com/auth/drive',
redirect_uri='http://127.0.0.1:8000/oauth2callback/'
)
FLOW.params['access_type'] = 'offline'
authorize_url = FLOW.step1_get_authorize_url()
return HttpResponseRedirect(authorize_url)
这里是 oauth2callback 函数。
def auth_return(request):
credential = FLOW.step2_exchange(request.GET)
return HttpResponseRedirect("/mycustomurl")
我使用 this 在 Django 服务器端启用 CORS。这是我在 Angular 中调用 oauth2 的服务部分。
(function () {
'use strict';
angular.module('myApp')
.service('myService', function ($http) {
this.saveToDrive = function (startYear, endYear, shape) {
var config = {
params: {
start: '1999',
end: '2002',
action: 'download-to-drive'
},
headers: {
'Access-Control-Allow-Origin': '*',
'X-Requested-With': null
}
}
var promise = $http.get('/oauth2/', config)
.then(function (response) {
return response.data;
});
return promise;
};
});
})();
请提出我在这里遗漏了什么。非常感谢任何帮助或建议。
我发现这是一个小设计问题,而不是代码问题。我分离了向客户端发送 oauth2 请求的逻辑,并且在 oauth2 请求之后,我使用 params
选项向内部 API 发送了请求。现在一切正常。