网站调用新浪微博内容(新浪微博验证之后可以获得AppSercet吗?(图))
优采云 发布时间: 2021-11-22 03:02网站调用新浪微博内容(新浪微博验证之后可以获得AppSercet吗?(图))
主要涉及到oauth2.0的概念。网上有很多这方面的资料,你可以google一下。
先到新浪微博申请服务器并创建应用: 然后到新浪微博开发平台申请成为开发者。在管理中心添加一个网站进行验证,如下图所示。网站名称:lijiejava软件开发,网站域名为sae中的应用。。
验证后可以获得App key和App Sercet。有了这两件事,你就可以开始编码了。
第一步:点击登录按钮,跳转到新浪微博提供的登录授权页面。网址是:
<br />"https://api.weibo.com/oauth2/authorize?client_id="+a*敏*感*词*ey+"&response_type=code&redirect_uri="+redirectURL;<br /><br />String redirectURL = "http://1.lijiecode.sinaapp.com/weibo/login";
主要参数:client_id 和redirect_uri。client_id 和 App Key 值,redirect_uri(回调接口),用于验证成功后在新浪微博上回调。这里redirect_uri和client_id必须对应,即redirect_uri需要是你在网站验证时输入的域名,否则会出现如下错误:
用户在此页面输入新浪微博的用户名和密码并同意授权。新浪微博服务器会回调redirect_uri接口并返回一个code值。
第二步:在回调接口中获取新浪微博服务器返回的code值。根据这个code值,可以得到accestoken和微博中用户的uid。
String url = "https://api.weibo.com/oauth2/access_token";<br /> Map postData = new HashMap();<br /> postData.put("grant_type", "authorization_code");<br /> postData.put("code", code);<br /> postData.put("redirect_uri", redirectURL);<br /> postData.put("client_id", a*敏*感*词*ey);<br /> postData.put("client_secret", appSercet);
返回的数据收录 access_token 和 uid 数据。
获取access_token和uid后,就可以通过如下url获取用户在新浪微博中的各种数据,包括用户名、位置、描述、头像等。
我在部署过程中遇到了2个问题:
1.因为我的网站只是验证没有审核,所以必须是我在后台添加的测试用户才能正确获取信息,否则会出现如下错误。对于未添加的用户,即使输入正确的用户名和密码也无法获取信息。:(
{"error":<br />"applications over the unaudited use restrictions!",<br />"error_code":21321,"request":"/2/users/show.json"}
2. 新浪sae不支持HttpClient,需要使用它提供的SaeFetchurl类。