android新浪微博开发-------授权部分实例+官方Api解析+可能遇到的问题

android新浪微博开发-------授权部分实例+官方Api解析+可能遇到的问题

一、解析说明官方api完善其不足之处
    <1>集成前准备
          在官方开放平台 创建应用
       1.1申请应用程序的APP_KEY
         1.2注册应用程序的包名和签名  获取签名我用的是官方提供的apk 装到手机上 输入包名就得到32位数字和小写字母组成的字符串(MD5值)
          1.3 其它查看官方Api说明文档

       <2>有两种方式来集成微博 SDK:
            1.  直接导入 weibosdkcore.jar:适用于只 需要授权、分享、网络请求 框架功能的项目
            2. 引用 WeiboSDK 工程(Library) :适用于 微博授权、分享,以及需要登陆按钮、调用 OpenAPI 的项目
        下面简述下两种导入方式的实现:
         方式一: 直接导入 weibosdkcore.jar
         将官方的提供 SDK JAR 包(weibosdkcore.jar)放到工程的 libs 目录下
          添加 JAR 包:工程右键propertiesjava build pathlibrariesadd external jar
        方式二: 引用 WeiboSDK 工程(Library)
         将WeiboSDK 工程整个目录拷贝到和你自己的工程相同的目录下
         在你自的工程中,添加 WeiboSDK 工程的引用:工程右键propertiesAndroidAdd选择工程,
       <3> Web授权需要在AndroidManifest.xml中,注册授权页面
              
<activity
android:name="com.sina.weibo.sdk.component.WeiboSdkBrowser
android:configChanges="keyboardHidden|orientation"
android:windowSoftInputMode="adjustResize"
android:exported="false" >
</activity>



  Ps:和经常引用第三方库方法一样
二、实例代码
   2.1 界面效果: 
                       
  2.2xml布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="23dp"
        android:layout_marginTop="56dp"
        android:text="客户端授权" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button1"
        android:layout_centerVertical="true"
        android:text="web授权" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button2"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="76dp"
        android:text="自动判断授权" />

</RelativeLayout>

2.3 java代码:
     2.3.1 常量类代码
    
/*
 * Copyright (C) 2010-2013 The SINA WEIBO Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.sinademo;
/**
 * 该类定义了微博授权时所需要的参数。
 * 
 * @author SINA
 * @since 2013-09-29
 */
public interface Constants {

	/** 当前 DEMO 应用的 APP_KEY,第三方应用应该使用自己的 APP_KEY 替换该 APP_KEY */
	public static final String APP_KEY = "3721449516";

	/**
	 * 当前 DEMO 应用的回调页,第三方应用可以使用自己的回调页。
	 * 
	 * <p>
	 * 注:关于授权回调页对移动客户端应用来说对用户是不可见的,所以定义为何种形式都将不影响, 但是没有定义将无法使用 SDK 认证登录。
	 * 建议使用默认回调页:https://api.weibo.com/oauth2/default.html
	 * </p>
	 */
	public static final String REDIRECT_URL = "https://api.weibo.com/oauth2/default.html";

	/**
	 * Scope 是 OAuth2.0 授权机制中 authorize 接口的一个参数。通过 Scope,平台将开放更多的微博
	 * 核心功能给开发者,同时也加强用户隐私保护,提升了用户体验,用户在新 OAuth2.0 授权页中有权利 选择赋予应用的功能。
	 * 
	 * 我们通过新浪微博开放平台-->管理中心-->我的应用-->接口管理处,能看到我们目前已有哪些接口的 使用权限,高级权限需要进行申请。
	 * 
	 * 目前 Scope 支持传入多个 Scope 权限,用逗号分隔。
	 * 
	 * 有关哪些 OpenAPI 需要权限申请,请查看:http://open.weibo.com/wiki/%E5%BE%AE%E5%8D%9AAPI
	 * 关于 Scope 概念及注意事项,请查看:http://open.weibo.com/wiki/Scope
	 */
	public static final String SCOPE = "email,direct_messages_read,direct_messages_write,"
			+ "friendships_groups_read,friendships_groups_write,statuses_to_me_read,"
			+ "follow_app_official_microblog," + "invitation_write";
}
2.3.2 java逻辑代码
  
package com.example.sinademo;

import com.sina.weibo.sdk.auth.AuthInfo;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.auth.WeiboAuthListener;
import com.sina.weibo.sdk.auth.sso.SsoHandler;
import com.sina.weibo.sdk.exception.WeiboException;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	private AuthInfo authInfo;
	private SsoHandler ssoHandler;
	private Oauth2AccessToken accessToken;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button khd = (Button) findViewById(R.id.button1);//手机装看微博客户端的授权
		Button web = (Button) findViewById(R.id.button2);//web 授权 无需安装客户端
		Button autoAll = (Button) findViewById(R.id.button3);
		authInfo = new AuthInfo(MainActivity.this, Constants.APP_KEY,
				Constants.REDIRECT_URL, Constants.SCOPE);
		ssoHandler = new SsoHandler(this, authInfo);

		web.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				ssoHandler.authorizeWeb(new MyAuthListener());

			}
		});
		//需要提交应用审核 认证才行
		khd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				ssoHandler.authorizeClientSso(new MyAuthListener());
			}
		});
		autoAll.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				ssoHandler.authorize(new MyAuthListener());
			}
		});
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	class MyAuthListener implements WeiboAuthListener {

		private Oauth2AccessToken accessToken;

		@Override
		public void onCancel() {
			// TODO Auto-generated method stub
			System.out.println("授权取消");

		}
  
		@SuppressWarnings("static-access")
		@Override
		public void onComplete(Bundle arg0) {
			// TODO Auto-generated method stub
			accessToken = Oauth2AccessToken.parseAccessToken(arg0);
			if (accessToken.isSessionValid()) {
				System.out.println(accessToken + "授权成功");
			} else {
				System.out.println("授权失败");
			}

		}

		@Override
		public void onWeiboException(WeiboException arg0) {
			// TODO Auto-generated method stub
			System.out.println("Auth Exception" + arg0.getMessage());
		}

	}
}

三、遇到的问题
    3.1

sso package or sign error

        新版微博客户端升级功能,无线应用使用sso授权必须填写包名、签名。否则会遇到sso package or sign error的报错,SSO授权暂时只针对iOS及Android端。
如果在使用SSO授权时,仍然发生sso package or sign error的报错,请根据以下方法自行排查:
(1)检查应用包名签名信息是否完善
如果你的应用只有一个包名、签名,请在 http://open.weibo.com/apps 下自己的应用中,进入应用信息-基本信息模块,点击“编辑”,即可看到填写Apple ID或Android包名的地方。填写完成后保存。该操作不需要提交审核,将立即生效。
如果你的应用有多个包名、签名,请提供您的appkey、各个包名、签名,私信至@微博开放平台,我们会安排相关同事,跟进帮助处理。
PS:签名一定是要通过平台提供的工具获取的签名

(2)检查是否已经设置了授权回调页地址
请在“我的应用 - 应用信息 - 高级信息”中填写您的应用回调页,这样才能使OAuth2.0授权正常进行。

(3)检查客户端版本及使用SDK版本
SDK v2.1已经发布了支持iPhone和Android的版本,需搭配官方客户端3.5.0及以上版本使用(微博客户端410以上,必须将第三方应用的包名签名信息在平台进行填写注册)。iPad端目前也可以下载使用SDK v2.1。


3.2 修改 debug.keystore

    见官方APi




<a href="http://www.cpv6.com/" _xhe_href="http://www.cpv6.com/" target="_blank">风车广告联盟</a>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值