Skip to content

Commit

Permalink
Exposed accessToken property - Version 1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaVitale committed Mar 28, 2018
1 parent daf9ef9 commit b21890e
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## 1.0.1
- Exposed property `accessToken` accessible after a valid `authorize`
- Added to [README](https://github.com/EveryUP/ti.linkedin/blob/master/README.md) an example on how to use the given `accessToken` on your server.

## 1.0.0
- Working authorization flow.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,23 @@ Here is a short login example:
Ti.API.info('User logged out.');
}

Do you want a more complete example? Look a the sample app.js.
Do you want a more complete example? Look a the sample [app.js](https://github.com/everyup/ti.linkedin/blob/master/example/app.js).

After a successful authorize, you can get a valid oauth access token via `linkedin.accessToken` property.<br />
As per the docs you can't forward and use this client token from your backend:

```
It is important to note that access tokens that are acquired via the Mobile SDK are only usable with the Mobile SDK, and cannot be used to make server-side REST API calls.
Similarly, access tokens that you already have stored from your users that authenticated using a server-side REST API call will not work with the Mobile SDK.
```

But there is an easy way to do this. <br />
Before to start a REST API call, simply set your request headers to:
```
"Authorization": "Bearer <YOUR_ACCESS_TOKEN>"
"x-li-src": "msdk"
```
and you will get a valid response.

## API

Expand All @@ -193,6 +209,7 @@ Do you want a more complete example? Look a the sample app.js.

#### Properties

- `accessToken` OAuth token set after a successful authorize
- `loggedIn` Indicates if the user is logged in
- `permissions` Array of permissions to request for your app. Be sure the permissions you want are set before calling authorize.

Expand Down
Binary file modified android/dist/ti.linkedin.jar
Binary file not shown.
Binary file modified android/libs/arm64-v8a/libti.linkedin.so
Binary file not shown.
Binary file modified android/libs/armeabi-v7a/libti.linkedin.so
Binary file not shown.
Binary file modified android/libs/x86/libti.linkedin.so
Binary file not shown.
2 changes: 1 addition & 1 deletion android/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 1.0.0
version: 1.0.1
apiversion: 4
architectures: arm64-v8a armeabi-v7a x86
description: ti.linkedin
Expand Down
10 changes: 10 additions & 0 deletions android/src/ti/linkedin/TiLinkedinModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ public boolean loggedIn() {
return LISessionManager.getInstance(activity).getSession().isValid();
}

@Kroll.getProperty
public String accessToken() {
Activity activity = TiApplication.getInstance().getCurrentActivity();

if (LISessionManager.getInstance(activity).getSession().isValid())
return LISessionManager.getInstance(activity).getSession().getAccessToken().getValue();

return null;
}

@Kroll.setProperty(name="permissions")
public void setPermissions(String[] permissions) {
this.permissions = new Permissions(permissions);
Expand Down
3 changes: 3 additions & 0 deletions iphone/Classes/TiLinkedinLoginManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
@interface TiLinkedinLoginManager : NSObject

@property(nonatomic, retain) NSArray* profileFields;
@property(nonatomic, retain) NSString* token;

+ (id)sharedInstance;
- (TiLinkedinLoginManager*)init;

- (void)authorize:(NSArray *)permissions successBlock:(void(^)(NSDictionary* payload))successHandler errorBlock:(void(^)(NSDictionary* payload))errorHandler;
- (NSString*)token;

@end
17 changes: 17 additions & 0 deletions iphone/Classes/TiLinkedinLoginManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@

@implementation TiLinkedinLoginManager

+ (id)sharedInstance {
static TiLinkedinLoginManager* sharedInstance = nil;
static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});

return sharedInstance;
}

- (TiLinkedinLoginManager*)init {
_profileFields = [NSArray arrayWithObjects:
@"id", @"first-name", @"last-name",
Expand All @@ -36,6 +47,8 @@ - (void)authorize:(NSArray *)permissions successBlock:(void(^)(NSDictionary* pay
NSDictionary* data = [NSJSONSerialization JSONObjectWithData:[[response data] dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
NSString* uid = [data objectForKey:@"id"];

_token = [[session accessToken] accessTokenValue];

successHandler(@{
@"success": NUMBOOL(YES),
@"canceled": NUMBOOL(NO),
Expand Down Expand Up @@ -63,6 +76,10 @@ - (void)authorize:(NSArray *)permissions successBlock:(void(^)(NSDictionary* pay
}];
}

- (NSString*)token {
return _token;
}

#pragma mark Internals

- (void)log:(NSString *)string forLevel:(NSString *)level {
Expand Down
1 change: 1 addition & 0 deletions iphone/Classes/TiLinkedinModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- (void)authorize:(__unused id)unused;
- (void)logout:(__unused id)unused;

- (NSString *_Nullable)accessToken;
- (NSNumber *_Nonnull) loggedIn;
- (NSArray<NSString*> *_Nullable) permissions;

Expand Down
10 changes: 8 additions & 2 deletions iphone/Classes/TiLinkedinModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,16 @@ - (void)handleOpenURL:(NSNotification *)notification {

#pragma mark Properties

- (NSNumber *)loggedIn {
- (NSNumber *_Nonnull)loggedIn {
return NUMBOOL([LISDKSessionManager hasValidSession]);
}

- (NSString *_Nullable)accessToken {
[self log:@"Getting access token" forLevel:@"info"];

return [[TiLinkedinLoginManager sharedInstance] token];
}

- (NSArray<NSString*> *_Nullable) permissions {
return _permissions;
}
Expand All @@ -90,7 +96,7 @@ - (void)initialize:(__unused id)unused {
- (void)authorize:(__unused id)unused {
ENSURE_UI_THREAD(authorize, unused);

TiLinkedinLoginManager* loginManager = [[TiLinkedinLoginManager alloc] init];
TiLinkedinLoginManager* loginManager = [TiLinkedinLoginManager sharedInstance];

[loginManager authorize:_permissions
successBlock:^(NSDictionary* payload) {
Expand Down
2 changes: 1 addition & 1 deletion iphone/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 1.0.0
version: 1.0.1
apiversion: 2
architectures: armv7 arm64 i386 x86_64
description: ti.linkedin
Expand Down
Binary file not shown.

0 comments on commit b21890e

Please sign in to comment.