&tag(GTMOAuth);
- (GTMOAuthAuthentication *)authForTwitter {
//以下を修正
NSString *myConsumerKey = @"YourConsumerKey";
NSString *myConsumerSecret = @"YourConsumerSecret";
if ([myConsumerKey length] == 0 || [myConsumerSecret length] == 0) {
return nil;
}
GTMOAuthAuthentication *auth;
auth = [[[GTMOAuthAuthentication alloc] initWithSignatureMethod:kGTMOAuthSignatureMethodHMAC_SHA1
consumerKey:myConsumerKey
privateKey:myConsumerSecret] autorelease];
[auth setServiceProvider:kTwitterServiceName];
return auth;
}
- (void)signInToTwitter {
[self signOut];
NSURL *requestURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/request_token"];
NSURL *accessURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/access_token"];
NSURL *authorizeURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/authorize"];
GTMOAuthAuthentication *auth = [self authForTwitter];
if (auth == nil) {
// perhaps display something friendlier in the UI?
NSAssert(NO, @"A valid consumer key and consumer secret are required for signing in to Twitter");
}
//これはダミーでいいらしい
[auth setCallback:@"http://www.example.com/OAuthCallback"];
NSString *keychainItemName = nil;
if ([self shouldSaveInKeychain]) {
keychainItemName = kTwitterKeychainItemName;
}
// Display the autentication view.
GTMOAuthViewControllerTouch *viewController;
viewController = [[[GTMOAuthViewControllerTouch alloc] initWithScope:nil //scopeはNULLに
language:nil
requestTokenURL:requestURL
authorizeTokenURL:authorizeURL
accessTokenURL:accessURL
authentication:auth
appServiceName:keychainItemName
delegate:self
finishedSelector:@selector(viewController:finishedWithAuth:error:)] autorelease];
// Cookieを消去するURLこれもhttpsバージョンにしたほうがよいのかな?
[viewController setBrowserCookiesURL:[NSURL URLWithString:@"https://api.twitter.com/"]];
[[self navigationController] pushViewController:viewController animated:YES];
}
- (void)accessFetcher:(GTMHTTPFetcher *)fetcher finishedWithData:(NSData *)data error:(NSError *)error {
[self setPendingFetcher:nil fetchType:nil];
if (error) {
[self invokeFinalCallbackWithError:error];
} else {
// we have an access token
[auth_ setKeysForResponseData:data];
[auth_ setHasAccessToken:YES];
#if !GTM_OAUTH_SKIP_GOOGLE_SUPPORT
if (shouldFetchGoogleUserInfo_
&& [[auth_ serviceProvider] isEqual:kGTMOAuthServiceProviderGoogle]) {
// fetch the user's information from the Google server
[self fetchGoogleUserInfo];
} else {
// we're not authorizing with Google, so we're done
[self invokeFinalCallbackWithError:nil];
}
#else
[self invokeFinalCallbackWithError:nil];
#endif
}
}
- (void)setKeysForResponseDictionary:(NSDictionary *)dict {
NSString *token = [dict objectForKey:kOAuthTokenKey];
if (token) {
[self setToken:token];
}
NSString *secret = [dict objectForKey:kOAuthTokenSecretKey];
if (secret) {
[self setTokenSecret:secret];
}
NSString *callbackConfirmed = [dict objectForKey:kOAuthCallbackConfirmedKey];
if (callbackConfirmed) {
[self setCallbackConfirmed:callbackConfirmed];
}
NSString *verifier = [dict objectForKey:kOAuthVerifierKey];
if (verifier) {
[self setVerifier:verifier];
}
NSString *provider = [dict objectForKey:kServiceProviderKey];
if (provider) {
[self setServiceProvider:provider];
}
NSString *email = [dict objectForKey:kUserEmailKey];
if (email) {
[self setUserEmail:email];
}
NSString *verified = [dict objectForKey:kUserEmailIsVerifiedKey];
if (verified) {
[self setUserEmailIsVerified:verified];
}
}
+ (BOOL)saveParamsToKeychainForName:(NSString *)appServiceName
authentication:(GTMOAuthAuthentication *)auth {
[self removeParamsFromKeychainForName:appServiceName];
// don't save unless we have a token that can really authorize requests
if (![auth hasAccessToken]) return NO;
// make a response string containing the values we want to save
NSString *password = [auth persistenceResponseString];
GTMOAuthKeychain *keychain = [GTMOAuthKeychain defaultKeychain];
return [keychain setPassword:password
forService:appServiceName
account:kGTLOAuthAccountName
error:nil];
}
_auth = [[GTMOAuthAuthentication alloc] initWithSignatureMethod:kGTMOAuthSignatureMethodHMAC_SHA1
consumerKey:kTwitterConsumerKey
privateKey:kTwitterConsumerSecret];
BOOL didAuth = [GTMOAuthViewControllerTouch authorizeFromKeychainForName:kTwitterKeychainItemName
authentication:_auth];
NSLog(@"didAuth=%d", didAuth);