Google api Google OAuth 2授权:使用AJAXP获得错误重定向\u uri\u不匹配
我已经在GoogleAPI Concole上设置了授权重定向URI,如下所示。 我使用localhost:64420/index获取代码,并将代码发送到localhost:64420/Auth以使用ajax post参数尝试获取访问令牌。 遗憾的是,我收到了错误消息:Google api Google OAuth 2授权:使用AJAXP获得错误重定向\u uri\u不匹配,google-api,google-oauth,google-search-console,Google Api,Google Oauth,Google Search Console,我已经在GoogleAPI Concole上设置了授权重定向URI,如下所示。 我使用localhost:64420/index获取代码,并将代码发送到localhost:64420/Auth以使用ajax post参数尝试获取访问令牌。 遗憾的是,我收到了错误消息: {error: "redirect_uri_mismatch", error_description: "Bad Request"} 以下是脚本: <script> var code
{error: "redirect_uri_mismatch", error_description: "Bad Request"}
以下是脚本:
<script>
var code = code;
var clientID = client_ID;
var clientSecret = client_Secret;
var redirect_uri = "http://localhost:64420/Report.aspx";
var searchurl = "https://www.googleapis.com/oauth2/v4/token";
$.ajax({
dataType: "json",
url: searchurl,
data: { code: code, client_id: clientID, client_secret: clientSecret, redirect_uri: redirect_uri, grant_type: 'authorization_code' },
type: "POST",
contentType: "application/x-www-form-urlencoded; charset=utf-8",
crossDomain: true,
cache: true,
success: function (data) {
alert(data);
},
error: function (jqXHR, exception, errorstr) {
console.log(jqXHR);
alert(errorstr);
}
});
</script>
var代码=代码;
var clientID=客户机ID;
var clientSecret=client\u Secret;
var redirect_uri=”http://localhost:64420/Report.aspx";
var searchurl=”https://www.googleapis.com/oauth2/v4/token";
$.ajax({
数据类型:“json”,
url:searchurl,
数据:{code:code,client\u id:clientID,client\u secret:clientSecret,redirect\u uri:redirect\u uri,grant\u type:'authorization\u code'},
类型:“POST”,
contentType:“application/x-www-form-urlencoded;charset=utf-8”,
跨域:是的,
是的,
成功:功能(数据){
警报(数据);
},
错误:函数(jqXHR、异常、errorstr){
console.log(jqXHR);
警报(errorstr);
}
});
应用程序中的重定向URL和API控制台中配置的重定向URL必须是字符匹配的精确字符。请记住,您可以在APi控制台中配置多个重定向URL,因此不要害羞,添加所有可能的变体,例如使用http和https。您真的应该使用https作为重定向URL,如果将来的更改不允许使用明文URL,我也不会感到惊讶。谢谢您的回复。我确信这两个重定向URI都是精确的字符匹配,我尝试使用https,但仍然得到相同的错误。请参阅: