我目前正在将PayPal REST API集成到我的iOS应用程序中,我需要使用Paypal登录来获取有关用户的一些信息。
我已经使用带有identity / openidconnect / userinfo /?schema = openid的访问令牌请求信息,并且我传入了访问令牌,我在响应中获得的最多是user_id字段。我必须遗漏一些简单的东西,但我如何获得其他信息?除了openID和futurePayments之外,我还将PayPalProfileSharingViewController的范围设置为包括电子邮件,个人资料和地址。我在开发人员仪表板中的应用程序还将配置文件信息检查为我要查询的字段。我已经尝试了沙箱和生产环境。但我只在响应中获得了user_id。
相关代码:
NSSet *scope = [NSSet setWithArray:@[kPayPalOAuth2ScopeOpenId, kPayPalOAuth2ScopeFuturePayments, kPayPalOAuth2ScopeEmail, kPayPalOAuth2ScopeProfile, kPayPalOAuth2ScopeAddress]];
PayPalConfiguration *config = [[PayPalConfiguration alloc] init];
config.merchantName = @"My Sample App";
config.merchantPrivacyPolicyURL = [NSURL URLWithString:@"http://www.google.com"];
config.merchantUserAgreementURL = [NSURL URLWithString:@"http://www.google.com"];
PayPalProfileSharingViewController *sharingVC = [[PayPalProfileSharingViewController alloc] initWithScopeValues:scope configuration:config delegate:self];
获取访问令牌的请求(这部分可行,我很确定):
- (void)fetchAccessTokenWithAuthToken:(NSString *)authToken
{
if (authToken && self.currentState == DFMPayPalManagerFetchingStateNone) {
self.currentState = DFMPayPalManagerFetchingStateFetchingRefreshAccessToken;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://%@/v1/oauth2/token", kPayPalProductionBaseURL]]];
NSMutableDictionary *requestHeaders = [NSMutableDictionary dictionary];
[requestHeaders setObject:@"application/x-www-form-urlencoded" forKey:@"Content-Type"];
NSString *authString = [NSString stringWithFormat:@"%@:%@", kPayPalProductionClientID, kPayPalProductionSecret];
[requestHeaders setObject:[NSString stringWithFormat:@"Basic %@", [[authString dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0]] forKey:@"Authorization"];
NSString *bodyString = [NSString stringWithFormat:@"grant_type=authorization_code&response_type=token&redirect_uri=urn:ietf:wg:oauth:2.0:oob&code=%@", authToken];
NSData *bodyData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:bodyData];
[request setAllHTTPHeaderFields:requestHeaders];
[request setHTTPMethod:@"POST"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
self.userInfoResponseData = [[NSMutableData alloc] init];
}
else {
// Error?
NSLog(@"Connection failed!");
self.currentState = DFMPayPalManagerFetchingStateNone;
}
}
}
最后,使用访问令牌获取信息的代码:
- (void)fetchUserInfoWithAccessToken:(NSString *)accessToken
{
if (accessToken && self.currentState == DFMPayPalManagerFetchingStateFetchingRefreshAccessToken) {
self.currentState = DFMPayPalManagerFetchingStateFetchingInfo;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://%@/v1/identity/openidconnect/userinfo/?schema=openid", kPayPalProductionBaseURL]]];
NSMutableDictionary *requestHeaders = [NSMutableDictionary dictionary];
[requestHeaders setObject:@"application/json" forKey:@"Content-Type"];
[requestHeaders setObject:[NSString stringWithFormat:@"Bearer %@", accessToken] forKey:@"Authorization"];
[request setAllHTTPHeaderFields:requestHeaders];
[request setHTTPMethod:@"GET"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
self.userInfoResponseData = [[NSMutableData alloc] init];
}
else {
// Error?
NSLog(@"Connection failed!");
self.currentState = DFMPayPalManagerFetchingStateNone;
}
}
}
结果:
2015-03-19 18:19:32.944 SampleApp[14675:209851] User Info received: {
"user_id" = "https://www.paypal.com/webapps/auth/identity/user/***";
}
非常感谢任何帮助。