gpt4 book ai didi

ios - 无法插入时刻-Google Plus API

转载 作者:行者123 更新时间:2023-12-01 18:14:21 24 4
gpt4 key购买 nike

我有一个iOS应用程序,可通过OAuth 2.0与Google Plus API进行身份验证。我遇到的问题是,即使我完全遵循Google文档,也无法发布自己的信息。这是我的POST请求:

NSString *gp_moment = [NSString stringWithFormat:@"https://www.googleapis.com/plus/v1/people/me/moments/vault?access_token=%@", token_gplus];
NSString *urlString = gp_moment;

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

// Set the header - Content-Type.
NSDictionary *the_header = @{@"Content-type" : @"application/json",
@"userId" : @"me",
@"collection" : @"vault",
@"requestvisibleactions" : @"http://schemas.google.com/AddActivity"};

[request setAllHTTPHeaderFields:the_header];

// Set the metadata for the GP Moment (eg: name). - request_visible_actions

NSDictionary *metadata = @{@"target" : @{
@"id" : @"replacewithuniqueidforaddtarget",
@"image" : @"http://www.google.com/s2/static/images/GoogleyEyes.png",
@"type": @"http://schema.org/CreativeWork",
@"description" : @"test_desc",
@"name" : @"TESTNAME",
},
@"type" : @"http://schemas.google.com/AddActivity"};

// Convert metadata into JSON format and submit.
NSError *jError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:metadata options:NSJSONWritingPrettyPrinted error:&jError];
[request setHTTPBody:jsonData];

NSHTTPURLResponse *response = nil;
NSError *error = nil;

NSData *returnedData;
returnedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSDictionary *headers = [response allHeaderFields];

NSLog(@"DATA: %@", [[NSString alloc] initWithData:returnedData encoding:NSASCIIStringEncoding]);
NSLog(@"%@", headers);
NSLog(@"%@", response);
NSLog(@"%@", error);

更新-我如何获取通行证

我正在使用Google提供的GTMOAuth2 iOS库获取 token 。它有4个我正在使用的主要类:

这是我从类实现到ViewController中的代码:
-(GTMOAuth2Authentication *)begin_authorization {

// Set the token URL to the token endpoint.
NSURL *tokenURL;

// Set a bogus redirect URI. It won't actually be used as the redirect will
// be intercepted by the OAuth library and handled in the app.
NSString *redirectURI;

GTMOAuth2Authentication *auth;
tokenURL = [NSURL URLWithString:@"https://accounts.google.com/o/oauth2/token"];
redirectURI = @"http://localhost:3000/oauth/callback/";

auth = [GTMOAuth2Authentication authenticationWithServiceProvider:@"GooglePlus" tokenURL:tokenURL redirectURI:redirectURI clientID:kMyClientID_gplus clientSecret:kMyClientSecret_gplus];
[auth setScope:@"https://www.googleapis.com/auth/plus.login"];

// Set the appropriate token type.
[auth setTokenType:@"Bearer"];

return auth;
}

-(void)authorize:(NSString *)service {
GTMOAuth2Authentication *auth = [self begin_authorization];

// Prepare the Authorization URL. We will pass in the name of the service
// that we wish to authorize with.
NSURL *authURL;
authURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://accounts.google.com/o/oauth2/auth/"]];

NSString *keyname;
keyname = [NSString stringWithFormat:@"GooglePlus"];

// Display the authentication view
GTMOAuth2ViewControllerTouch *viewController;
viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithAuthentication:auth authorizationURL:authURL keychainItemName:keyname delegate:self finishedSelector:@selector(viewController:finishedWithAuth:error:)];

[viewController setBrowserCookiesURL:[NSURL URLWithString:@"https://accounts.google.com/o/oauth2/auth/"]];

// Push the authentication view to our navigation controller instance
[[self navigationController] pushViewController:viewController animated:YES];
}

-(void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error {

if (error != nil) {
// Authentication failed
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Authorization Failed" message:[error localizedDescription] delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[alertView show];
}

else {
// Authentication succeeded
// Assign the access token to the instance property for later use

account_check = auth.accessToken;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:account_check forKey:@"gp_token"];
[defaults synchronize];
}
}

在GTMOAuth2SignIn.m文件中,我还设置如下的offline_access和“requestvisisbleactions”参数:
NSMutableDictionary *paramsDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"code", @"response_type",
clientID, @"client_id",
scope, @"scope", // scope may be nil
@"force", @"approval_prompt",
@"offline", @"access_type",
@"http://schemas.google.com/AddActivity", @"requestvisibleactions",
nil];

更新-2

我尝试浏览所有Google Plus文档以及“requestvisisbleactions”和“request_visisble_actions”之间的区别,但仍然没有任何效果。有人知道哪里出问题了吗?

更新-3-我仍然收到的错误

我一直从Google Plus API收到此错误。我只是不知道为什么...:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "unauthorized",
"message": "Unauthorized"
}
],
"code": 401,
"message": "Unauthorized"
}
}

最佳答案

我相信您需要使用request_visible_actions。因此,您的参数可能设置为

NSMutableDictionary *paramsDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"code", @"response_type",
clientID, @"client_id",
scope, @"scope", // scope may be nil
@"force", @"approval_prompt",
@"offline", @"access_type",
@"http://schemas.google.com/AddActivity", @"request_visible_actions",
nil];

关于ios - 无法插入时刻-Google Plus API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24061521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com