#import <CommonCrypto/CommonHMAC.h>
#import <CommonCrypto/CommonCryptor.h>
#import "Base64.h"
#define SINA_T_HOST
@"api.t.sina.com.cn"//api.t.sina.com.cn
#define SINA_WEIBO_APP_KEY @"YOUR APP KEY"
#define SECRET @"YOUR APP SECRET"
#define OAUTH_VERSION @"1.0"
#define OAUTH_SIGNATURE_METHOD @"HMAC-SHA1"
#pragma mark 获得时间戳
- (NSString *)_generateTimestamp
{
return [NSString stringWithFormat:@"%d", time(NULL)];
}
#pragma mark 获得随时字符串 // 这个可以获取随机数,不一定要用uuid的
- (NSString *)_generateNonce
{
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
NSMakeCollectable(theUUID);
return (NSString *)string;
}
#pragma mark 获取request_token的参数
-(void) getRequestToken {
NSString *baseUrl = [NSString stringWithFormat:@"http://%@/oauth/request_token", SINA_T_HOST];
NSString *nonce = [self _generateNonce];
NSString *timestamp = [self _generateTimestamp];
NSMutableDictionary* info = [NSMutableDictionary dictionaryWithObjectsAndKeys:
SINA_WEIBO_APP_KEY,@"oauth_consumer_key",
OAUTH_SIGNATURE_METHOD,@"oauth_signature_method",
timestamp,@"oauth_timestamp",
nonce,@"oauth_nonce",
OAUTH_VERSION,@"oauth_version",nil];
[self hmac_sha1_signature:@"GET" url:baseUrl param:info token_secret:@""]; // 获取签名
NSString *oauthHeader = [NSString stringWithFormat:@"OAuth realm="%@", oauth_consumer_key="%@", oauth_signature_method="%@", oauth_signature="%@", oauth_timestamp="%@", oauth_nonce="%@", oauth_version="1.0"",
@"",
[info valueForKey:@"oauth_consumer_key"],
[info valueForKey:@"oauth_signature_method"],
[info valueForKey:@"oauth_signature"],
[info valueForKey:@"oauth_timestamp"],
[info valueForKey:@"oauth_nonce"]];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:baseUrl]];
[theRequest setHTTPMethod:@"GET"];
[theRequest setValue:oauthHeader forHTTPHeaderField:@"Authorization"];
self.connect = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; // 开始连接 返回值在
}
// 获取request_token之后,要做一件事情,就是让用户登录,调出新浪微博登录页面:
- (NSString*)authorizeUrl{
//
NSString *baseUrl = [NSString stringWithFormat:@"http://%@/oauth/authorize", SINA_T_HOST];
NSString *url = [NSString stringWithFormat:@"%@?oauth_token=%@&oauth_token_secret=%@&oauth_callback=%@", baseUrl, self.oauth
#define SINA_WEIBO_APP_KEY @"YOUR APP KEY"
#define SECRET @"YOUR APP SECRET"
#define OAUTH_VERSION @"1.0"
#define OAUTH_SIGNATURE_METHOD @"HMAC-SHA1"
#pragma mark 获得时间戳
- (NSString *)_generateTimestamp
{
return [NSString stringWithFormat:@"%d", time(NULL)];
}
#pragma mark 获得随时字符串 // 这个可以获取随机数,不一定要用uuid的
- (NSString *)_generateNonce
{
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
NSMakeCollectable(theUUID);
return (NSString *)string;
}
#pragma mark 获取request_token的参数
-(void) getRequestToken {
NSString *baseUrl = [NSString stringWithFormat:@"http://%@/oauth/request_token", SINA_T_HOST];
NSString *nonce = [self _generateNonce];
NSString *timestamp = [self _generateTimestamp];
NSMutableDictionary* info = [NSMutableDictionary dictionaryWithObjectsAndKeys:
SINA_WEIBO_APP_KEY,@"oauth_consumer_key",
OAUTH_SIGNATURE_METHOD,@"oauth_signature_method",
timestamp,@"oauth_timestamp",
nonce,@"oauth_nonce",
OAUTH_VERSION,@"oauth_version",nil];
[self hmac_sha1_signature:@"GET" url:baseUrl param:info token_secret:@""]; // 获取签名
NSString *oauthHeader = [NSString stringWithFormat:@"OAuth realm="%@", oauth_consumer_key="%@", oauth_signature_method="%@", oauth_signature="%@", oauth_timestamp="%@", oauth_nonce="%@", oauth_version="1.0"",
@"",
[info valueForKey:@"oauth_consumer_key"],
[info valueForKey:@"oauth_signature_method"],
[info valueForKey:@"oauth_signature"],
[info valueForKey:@"oauth_timestamp"],
[info valueForKey:@"oauth_nonce"]];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:baseUrl]];
[theRequest setHTTPMethod:@"GET"];
[theRequest setValue:oauthHeader forHTTPHeaderField:@"Authorization"];
self.connect = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; // 开始连接 返回值在
}
// 获取request_token之后,要做一件事情,就是让用户登录,调出新浪微博登录页面:
- (NSString*)authorizeUrl{
//
NSString *baseUrl = [NSString stringWithFormat:@"http://%@/oauth/authorize", SINA_T_HOST];
NSString *url = [NSString stringWithFormat:@"%@?oauth_token=%@&oauth_token_secret=%@&oauth_callback=%@", baseUrl, self.oauth