http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 25693|回复: 15

[接口] OA系统集成腾讯企业邮箱模块获取access_token时报错

[复制链接]
发表于 2014-5-5 17:43:11 | 显示全部楼层 |阅读模式
第一次接触腾讯的接口开发,不是很熟悉; 后台程序是用asp.net+c#写的,程序第一步是要获取token ,在执行调试的时候总是报了一个错误   System.Net.WebException   {"远程服务器返回错误: (400) 错误的请求。"},不知道是哪里的原因,知道的朋友能否给点例子参考一下,谢谢


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2014-5-5 17:51:44 | 显示全部楼层
这个一般是请求的参数或者是头信息不对,最好是能看下你请求的代码,建议使用我的Httphelper类
发表于 2014-5-5 22:28:13 | 显示全部楼层
你可以先试试,正常通过浏览请求,看看是否正常,如果浏览器请求正常的话,你在抓包看看,抓包后,主要对比你正常的请求,和你C#软件请求的不同之处,看看问题出在那里。
 楼主| 发表于 2014-5-6 09:49:52 | 显示全部楼层
站长苏飞 发表于 2014-5-5 17:51
这个一般是请求的参数或者是头信息不对,最好是能看下你请求的代码,建议使用我的Httphelper类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Text;
using LitJson;
using System.Runtime.Serialization.Json;
using System.Web.Script.Serialization;
using System.ServiceModel.Web;
using System.Net.Security;
using System.Security;

namespace WebApplication1
{
    public partial class WebForm2 : System.Web.UI.Page
    {
         string client_id = "abc.com"; //注册的管理员帐号@后面的部分(能否理解为域名?)
         string client_secret = "abcdefg000111";//通过帐号获取的key(key已经获取,这里只是举例写了一个别的)
         string alias = "admin@abc.com";//腾讯企业邮箱登录用的邮箱帐号
         protected void btnOAuth_Click(object sender, EventArgs e)
        {
            try
            {
                string postUrl = "https://exmail.qq.com/cgi-bin/token"; //要发送的HTTPS地址
                string postData = "grant_type=client_credentials&client_id=abc.com&client_secret=abcdefg000111";//要与HTTPS一起发送的参数
           
                HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(postUrl);
                Request.Method = "POST";
                Request.ContentType = "application/x-www-form-urlencoded";
                byte[] data = Encoding.UTF8.GetBytes(postData);
                Stream newStream = Request.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                HttpWebResponse response = (HttpWebResponse)Request.GetResponse();//程序允许到这里就报错了 就是我帖子里描述的: 远程服务器返回错误: (400) 错误的请求。
                Stream stream = response.GetResponseStream();
                StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8, true);

                txtOAuth.Text = reader.ReadToEnd();//请求成功后返回的是JSON格式的数据, 这里只是简单的先看一下有没有数据返回,后期还得找方法解析JSON数据,楼主要是有类似的方法希望给点实例吧,谢谢了
            }
            catch (Exception ex)
            {
                txtOAuth.Text = ex.Message;
            }
        }
    }
}

//上面这些代码是我上网根据查的一些资料写的,不知道是不是少什么方法,请楼主给点提示什么的吧,折腾好几天了没结果
 楼主| 发表于 2014-5-6 09:50:07 | 显示全部楼层
站长苏飞 发表于 2014-5-5 17:51
这个一般是请求的参数或者是头信息不对,最好是能看下你请求的代码,建议使用我的Httphelper类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Text;
using LitJson;
using System.Runtime.Serialization.Json;
using System.Web.Script.Serialization;
using System.ServiceModel.Web;
using System.Net.Security;
using System.Security;

namespace WebApplication1
{
    public partial class WebForm2 : System.Web.UI.Page
    {
         string client_id = "abc.com"; //注册的管理员帐号@后面的部分(能否理解为域名?)
         string client_secret = "abcdefg000111";//通过帐号获取的key(key已经获取,这里只是举例写了一个别的)
         string alias = "admin@abc.com";//腾讯企业邮箱登录用的邮箱帐号
         protected void btnOAuth_Click(object sender, EventArgs e)
        {
            try
            {
                string postUrl = "https://exmail.qq.com/cgi-bin/token"; //要发送的HTTPS地址
                string postData = "grant_type=client_credentials&client_id=abc.com&client_secret=abcdefg000111";//要与HTTPS一起发送的参数
           
                HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(postUrl);
                Request.Method = "POST";
                Request.ContentType = "application/x-www-form-urlencoded";
                byte[] data = Encoding.UTF8.GetBytes(postData);
                Stream newStream = Request.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                HttpWebResponse response = (HttpWebResponse)Request.GetResponse();//程序允许到这里就报错了 就是我帖子里描述的: 远程服务器返回错误: (400) 错误的请求。
                Stream stream = response.GetResponseStream();
                StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8, true);

                txtOAuth.Text = reader.ReadToEnd();//请求成功后返回的是JSON格式的数据, 这里只是简单的先看一下有没有数据返回,后期还得找方法解析JSON数据,楼主要是有类似的方法希望给点实例吧,谢谢了
            }
            catch (Exception ex)
            {
                txtOAuth.Text = ex.Message;
            }
        }
    }
}

//上面这些代码是我上网根据查的一些资料写的,不知道是不是少什么方法,请楼主给点提示什么的吧,折腾好几天了没结果非常感谢你帮了我的大忙,真的太感谢你啦!
发表于 2014-5-6 09:53:45 | 显示全部楼层
这个接口没写过,你确定你的网址是对的,参数是对的吗?貌似像是网址不对
 楼主| 发表于 2014-5-6 10:27:08 | 显示全部楼层
站长苏飞 发表于 2014-5-6 09:53
这个接口没写过,你确定你的网址是对的,参数是对的吗?貌似像是网址不对

噢,腾讯企业邮箱有一个相关文档,里面是这样的写的。
根据申请到的 client_id 和 client_secret,采用 ClientCredentials 方式获取 access_token。
其中 client_id 为管理员帐号,client_secret 为管理端开放接口申请到的 key。
假设 client_id 为 biz0876xa,client_secret 为 yuw_0dfuxUa。
请求示例如下:
POST https://exmail.qq.com/cgi-bin/token HTTP /1.1
Host: exmail.qq.com
Content-Length: 75
grant_type=client_credentials&client_id=biz0876xa&client_secret=yuw_0dfuxUa
或者:
POST https://exmail.qq.com/cgi-bin/token HTTP /1.1
Host: exmail.qq.com
Authorization: Basic Yml6MDg3NnhhOnl1d18wZGZ1eFVh
Content-Length: 29
grant_type=client_credentials
如果验证通过,返回:
{
"access_token":"jIFA9ju6v5XP",
"token_type":"Bearer",
"expires_in":86400,
"refresh_token":""
}

相关文档链接:http://wenku.baidu.com/view/43298d0cfe4733687e21aa7f.html
 楼主| 发表于 2014-5-6 10:37:33 | 显示全部楼层
...... 发表于 2014-5-5 22:28
你可以先试试,正常通过浏览请求,看看是否正常,如果浏览器请求正常的话,你在抓包看看,抓包后,主要对比 ...

将这一行HTTPS链接(https://exmail.qq.com/cgi-bin/to ... p;client_secret=xxx) 直接输入浏览器地址栏中请求吗?我刚才用360浏览器又试了一下,返回的内容为:{
"error":"invalid_request"
}
用IE9.0也试了一下 返回的网页标题为:HTTP 400 错误的请求,网页中的“详细信息”内容为:此错误(HTTP 400 错误的请求)意味着 Internet Explorer 可以连接到 Web 服务器,但是因为地址问题无法找到该网页。不知道什么原因?
发表于 2014-6-30 18:22:38 | 显示全部楼层
zuoyexingchen 发表于 2014-5-6 10:37
将这一行HTTPS链接(https://exmail.qq.com/cgi-bin/token?grant_type=client_credentials&client_id=abc ...

管理账户不对;
http://wenku.baidu.com/view/69050447be1e650e52ea990b.html?re=view&qq-pf-to=pcqq.c2c
发表于 2014-9-9 22:59:40 | 显示全部楼层
我只是路过打酱油的。
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-20 14:54

© 2014-2021

快速回复 返回顶部 返回列表